{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reformat The String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reformat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #重新格式化字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个混合了数字和字母的字符串 <code>s</code>，其中的字母均为小写英文字母。</p>\n",
    "\n",
    "<p>请你将该字符串重新格式化，使得任意两个相邻字符的类型都不同。也就是说，字母后面应该跟着数字，而数字后面应该跟着字母。</p>\n",
    "\n",
    "<p>请你返回 <strong>重新格式化后</strong> 的字符串；如果无法按要求重新格式化，则返回一个 <strong>空字符串</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;a0b1c2&quot;\n",
    "<strong>输出：</strong>&quot;0a1b2c&quot;\n",
    "<strong>解释：</strong>&quot;0a1b2c&quot; 中任意两个相邻字符的类型都不同。 &quot;a0b1c2&quot;, &quot;0a1b2c&quot;, &quot;0c2a1b&quot; 也是满足题目要求的答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;leetcode&quot;\n",
    "<strong>输出：</strong>&quot;&quot;\n",
    "<strong>解释：</strong>&quot;leetcode&quot; 中只有字母，所以无法满足重新格式化的条件。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;1229857369&quot;\n",
    "<strong>输出：</strong>&quot;&quot;\n",
    "<strong>解释：</strong>&quot;1229857369&quot; 中只有数字，所以无法满足重新格式化的条件。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;covid2019&quot;\n",
    "<strong>输出：</strong>&quot;c2o0v1i9d&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;ab123&quot;\n",
    "<strong>输出：</strong>&quot;1a2b3&quot;\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 500</code></li>\n",
    "\t<li><code>s</code> 仅由小写英文字母和/或数字组成。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reformat-the-string](https://leetcode.cn/problems/reformat-the-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reformat-the-string](https://leetcode.cn/problems/reformat-the-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"a0b1c2\"', '\"leetcode\"', '\"1229857369\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month_names: List[str] = [\n",
    "            \"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\",\n",
    "            \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\",\n",
    "        ]\n",
    "        month_number: Dict[str, int] = {\n",
    "            m: c for m, c in zip(month_names, range(1, 13))\n",
    "        }\n",
    "\n",
    "        day, month, year = date.split()\n",
    "        year = int(year)\n",
    "        month = month_number[month]\n",
    "        day = int(day[:-2])\n",
    "        \n",
    "        return \"{:04d}-{:02d}-{:02d}\".format(year, month, day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        datelist = date.split(\" \")\n",
    "        year = str(datelist[2])\n",
    "        month = datelist[1]\n",
    "        day = datelist[0]\n",
    "        m1 = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        new_month=m1.index(month)+1\n",
    "        if new_month<10:\n",
    "            new_month = \"0\"+str(new_month)\n",
    "        else:\n",
    "            new_month = str(new_month)\n",
    "        new_day = day[:-2]\n",
    "        if len(new_day)==1:\n",
    "            new_day=\"0\"+new_day\n",
    "        return year+\"-\"+new_month+\"-\"+new_day   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month,year=date.split()\n",
    "        dic={\"Jan\":1, \"Feb\":2, \"Mar\":3, \"Apr\":4, \"May\":5, \"Jun\":6, \"Jul\":7, \"Aug\":8, \"Sep\":9, \"Oct\":10, \"Nov\":11, \"Dec\":12}\n",
    "        return \"%s-%02d-%02d\" %(year,dic[month],int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        map = {\"Jan\": \"1\", \"Feb\": \"2\", \"Mar\":\"3\", \"Apr\": \"4\", \"May\": \"5\",\n",
    "               \"Jun\": \"6\", \"Jul\": \"7\", \"Aug\": \"8\", \"Sep\": \"9\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n",
    "        date = date.split(' ')\n",
    "        date.reverse()\n",
    "        date[2] = date[2][:-2].rjust(2, '0')\n",
    "        date[1] = map[date[1]].rjust(2, '0')\n",
    "        return '-'.join(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day, month, year = date.split(\" \")\n",
    "        dic={'Jan':'01',\"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\",\n",
    "         \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        day = day[:-2]\n",
    "        if len(day) == 1:\n",
    "            day=\"0\"+day\n",
    "        return \"-\".join([year, dic[month],day])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date = date.split(' ')\n",
    "        if len(date[0]) == 3:\n",
    "            day = date[0][0]\n",
    "            day = '0' + str(day)\n",
    "        else:\n",
    "            day = date[0][0:2]\n",
    "        months = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}\n",
    "        mon = months[date[1]]\n",
    "        year = date[2]\n",
    "        return '-'.join([year, mon, day])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date_li = date.split()\n",
    "        months_set = [\"\",\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        days_set = [\"\", \"1st\", \"2nd\", \"3rd\", \"4th\", \"5th\", \"6th\", \"7th\", \"8th\", \"9th\", \"10th\", \"11th\", \"12th\", \"13th\", \"14th\", \"15th\", \"16th\", \"17th\", \"18th\", \"19th\", \"20th\", \"21st\", \"22nd\", \"23rd\", \"24th\", \"25th\", \"26th\", \"27th\", \"28th\", \"29th\", \"30th\", \"31st\"]\n",
    "        year, month, day = date_li[-1], months_set.index(date_li[1]), days_set.index(date_li[0])\n",
    "        # day = '0' + day if len(day)==1 else day\n",
    "        # month = '0' + month if len(month)==1 else month\n",
    "        # return year + '-' + month + '-' + day\n",
    "        return \"%s-%02d-%02d\" % (year, month, day)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,mon,year=date.split()\n",
    "        mon_lst = [\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"]\n",
    "        mon_map={mon:idx for idx,mon in enumerate(mon_lst,1)}\n",
    "        return \"%s-%02d-%02d\" % (year,mon_map[mon],int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        li = date.split()  # 分割字符串\n",
    "        day = li[0][:-2] if len(li[0][:-2]) > 1 else '0' + li[0][:-2]  # 得到day\n",
    "        dic = {\"Jan\":1, \"Feb\":2, \"Mar\":3, \"Apr\":4, \"May\":5, \"Jun\":6, \"Jul\":7, \"Aug\":8, \"Sep\":9, \"Oct\":10, \"Nov\":11, \"Dec\":12}\n",
    "        month = str(dic[li[1]]) if len(str(dic[li[1]])) > 1 else '0' + str(dic[li[1]]) # 得到month\n",
    "        return li[2] + '-' + month + '-' + day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "MONTH = [0,\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date = date.split()\n",
    "        day = date[0][:-2]\n",
    "        month = MONTH.index(date[1])\n",
    "        year = date[2]\n",
    "        return f'{year}-{month:02}-{day:>02}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        Month = {\n",
    "            \"Jan\":'01', \n",
    "            \"Feb\":'02', \n",
    "            \"Mar\":'03', \n",
    "            \"Apr\":'04', \n",
    "            \"May\":'05', \n",
    "            \"Jun\":'06', \n",
    "            \"Jul\":'07', \n",
    "            \"Aug\":'08', \n",
    "            \"Sep\":'09', \n",
    "            \"Oct\":'10', \n",
    "            \"Nov\":'11', \n",
    "            \"Dec\":'12'\n",
    "        }\n",
    "        res = date.split(' ')[-1] + '-' + Month[date.split(' ')[1]] + '-'\n",
    "        res += date.split(' ')[0][:-2] if len(date.split(' ')[0]) > 3 else ('0' + date.split(' ')[0][:-2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        dic = {\"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n",
    "        date=date.split(\" \")\n",
    "        print(date)\n",
    "        ans=[0]*3\n",
    "        ans[2]=date[0][:-2].zfill(2)\n",
    "        ans[1]=dic.get(date[1])\n",
    "        ans[0]=date[2]\n",
    "        print(ans)\n",
    "        return \"-\".join(ans)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ddate=date.split()\n",
    "        mmon={\"Jan\":'01', \"Feb\":'02', \"Mar\":'03', \"Apr\":'04', \"May\":'05', \"Jun\":'06', \"Jul\":'07', \"Aug\":'08', \"Sep\":'09', \"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        t=[x for x in ddate[0] if x.isdigit() ]        \n",
    "        ddate[0]='{:02d}'.format(int(''.join(t)))\n",
    "        ddate[1]=mmon[ddate[1]]\n",
    "        #print(ddate)\n",
    "        return ddate[2]+'-'+ddate[1]+'-'+ddate[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date = date.split(' ')\n",
    "        months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        year = date[2]\n",
    "        month = months.index(date[1])+1\n",
    "        if len(date[0])==3:\n",
    "            day = int(date[0][0])\n",
    "        else:\n",
    "            day = int(date[0][:2])\n",
    "        out = str(year) + '-' + str(month).zfill(2) + '-' + str(day).zfill(2)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ls = date.split(' ')[::-1]\n",
    "        m = {\"Jan\":'01', \"Feb\":'02', \"Mar\":'03', \"Apr\":'04', \"May\":'05', \"Jun\":'06', \n",
    "             \"Jul\":'07', \"Aug\":'08', \"Sep\":'09', \"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        if len(ls[2]) == 3:\n",
    "            ls[2] = '0'+ls[2]\n",
    "        return ls[0] + '-' + m[ls[1]] + '-' + ls[2][:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][: -2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/reformat-date/solutions/336420/zhuan-bian-ri-qi-ge-shi-by-leetcode-solution/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        dic = {\"Jan\": '01', \"Feb\": '02', \"Mar\": '03', \"Apr\": '04', \"May\": '05', \"Jun\": '06', \"Jul\": '07', \"Aug\": '08', \"Sep\": '09', \"Oct\": '10', \"Nov\": '11', \"Dec\": '12'}\n",
    "        year = date[-4:]\n",
    "        month = date[-8: -5]\n",
    "        day = date[: -11]\n",
    "        if len(day) == 1:\n",
    "            day = '0' + day\n",
    "        res = year + '-' + dic[month] + '-' + day\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        year = date[len(date)-4:len(date):1]\n",
    "        dic={\"Jan\":'01',\"Feb\":'02',\"Mar\":'03',\"Apr\":'04',\"May\":'05',\"Jun\":'06',\"Jul\":'07',\"Aug\":'08',\"Sep\":'09',\"Oct\":10,\"Nov\":'11',\"Dec\":'12'}\n",
    "        month = dic[date[len(date)-8:len(date)-5:1]]\n",
    "        if len(date)==13:\n",
    "            day = date[0:2:1]\n",
    "        else:\n",
    "           date = '0' + date[0]\n",
    "           day = date[0:2:1]\n",
    "        return year+\"-\"+str(month)+\"-\"+day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date = date.split(' ')\n",
    "        day = \"\"\n",
    "        for ch in date[0]:\n",
    "            if ch.isalpha():break\n",
    "            else:day += ch\n",
    "        if len(day)==1:dd = '0'+day\n",
    "        else:dd = day\n",
    "        month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        mon = date[1]\n",
    "        if len(str(month.index(mon)+1))==1:mm = '0'+str(month.index(mon)+1)\n",
    "        else:mm = str(month.index(mon)+1)\n",
    "        year = date[2]\n",
    "        return year+'-'+mm+'-'+dd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        dic = {\"Jan\":\"01\", \"Feb\":\"02\",\"Mar\":\"03\",\"Apr\":\"04\",\"May\":\"05\",\"Jun\":\"06\",\"Jul\":\"07\",\"Aug\":\"08\",\"Sep\":\"09\",\"Oct\":\"10\",\"Nov\":\"11\",\"Dec\":\"12\"}\n",
    "        year = date[-4:]\n",
    "        month = dic[date[-8:-5]]\n",
    "        day = date[:-11]\n",
    "        if len(day) == 1:\n",
    "            day = \"0\"+day\n",
    "        return year+\"-\"+month+\"-\"+day\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        segs = re.search(r'(\\d{1,2}).*?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*?(\\d{4})', date, flags=re.I).groups()\n",
    "        day, month, year = segs\n",
    "        month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"].index(month) + 1\n",
    "        return datetime(int(year), month, int(day)).strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        d=date.split(' ')\n",
    "        a={'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}\n",
    "        s=''\n",
    "        for i in d[0]:\n",
    "            if i.isdigit():\n",
    "                s+=i\n",
    "        if len(s)==1:\n",
    "            s='0'+s\n",
    "        return d[-1]+'-'+a[d[1]]+'-'+s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ans = date.split(\" \")\n",
    "        \n",
    "        Month={\n",
    "            \"Jan\": \"01\",\n",
    "            \"Feb\": \"02\",\n",
    "            \"Mar\": \"03\",\n",
    "            \"Apr\":\"04\",\n",
    "            \"May\":\"05\",\n",
    "            \"Jun\":\"06\",\n",
    "            \"Jul\":\"07\",\n",
    "            \"Aug\":\"08\",\n",
    "            \"Sep\":\"09\",\n",
    "            \"Oct\":\"10\",\n",
    "            \"Nov\":\"11\",\n",
    "            \"Dec\":\"12\"\n",
    "        }\n",
    "        ans[2] = ans[2] + \"-\"+Month[ans[1]]\n",
    "        if len(ans[0]) == 3:\n",
    "            ans[2] = ans[2] + \"-0\"+ans[0][0:1]\n",
    "        else:\n",
    "            ans[2] = ans[2] + \"-\"+ans[0][0:2]\n",
    "        return ans[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "\n",
    "        date = date.split(' ')\n",
    "\n",
    "        monthDict = dict(zip([\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"],[str(i) if i >= 10 else '0' + str(i) for i in range(1,13,1)]))\n",
    " \n",
    "        return date[2] + '-' + monthDict[date[1]] + '-' + ('' if len(date[0][ : -2]) == 2 else '0') + date[0][ : -2] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        d,m,y=date.split()\n",
    "        mon_lst = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        return '%s-%02d-%02d'%(y,mon_lst.index(m)+1,int(d[:-2]))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month,year = date.split()\n",
    "        months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        mon_map = {mon:idx for idx,mon in enumerate(months,1)}\n",
    "        # return \"{}-{:0>2d}-{:0>2d}\".format(year,mon_map[month], int(day[:-2]))\n",
    "        return \"%s-%02d-%02d\" % (year, mon_map[month], int(day[:-2]))\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        a = date.split(\" \")\n",
    "        ms = {\"Jan\":1, \"Feb\":2, \"Mar\":3, \"Apr\":4, \"May\":5, \"Jun\":6, \"Jul\":7, \"Aug\":8, \"Sep\":9, \"Oct\":10, \"Nov\":11, \"Dec\":12}\n",
    "        year = a[2]\n",
    "        m = ms[a[1]]\n",
    "        if m < 10:\n",
    "            m = \"0\" + str(m)\n",
    "        t = 0 \n",
    "        for c in a[0]:\n",
    "            if c.isdigit():\n",
    "                t = t * 10 + ord(c) - ord('0')\n",
    "        if t < 10:\n",
    "            t = \"0\" + str(t)\n",
    "\n",
    "        return f'{year}-{m}-{t}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date):\n",
    "        mon = {\"Jan\": '01', \"Feb\": '02', \"Mar\": '03', \"Apr\": '04', \"May\": '05', \"Jun\": '06',\n",
    "               \"Jul\": '07', \"Aug\": '08', \"Sep\": '09', \"Oct\": '10', \"Nov\": '11', \"Dec\": '12'}\n",
    "        date_list = date.split(' ')\n",
    "        date = ''\n",
    "        for word in date_list[0]:\n",
    "            if 48 <= ord(word) <= 57:\n",
    "                date += word\n",
    "        if len(date) == 1:\n",
    "            date = '0' + date\n",
    "        res = date_list[2]+'-'+mon[date_list[1]]+'-'+date\n",
    "        return res\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month,year = date.split()\n",
    "        months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        # return \"{}-{:0>2d}-{:0>2d}\".format(year,months.index(month)+1, int(day[:-2]))\n",
    "        return \"%s-%02d-%02d\" % (year, months.index(month)+1, int(day[:-2]))\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        Month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        res = \"\"\n",
    "        YYYY = date[-4:]\n",
    "        if len(date) == 13:\n",
    "            MM = Month.index(date[5:8])+1\n",
    "            DD = date[0:2]\n",
    "        else:\n",
    "            MM = Month.index(date[4:7])+1\n",
    "            DD = date[0:1]\n",
    "        if MM < 10:\n",
    "            MM = \"0\"+str(MM)\n",
    "        else:\n",
    "            MM = str(MM)\n",
    "        if int(DD) < 10:\n",
    "            DD = \"0\" + str(DD)\n",
    "        return YYYY + \"-\" + MM + \"-\" + DD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        year = date[len(date)-4:len(date):1]\n",
    "        dic={\"Jan\":'01',\"Feb\":'02',\"Mar\":'03',\"Apr\":'04',\"May\":'05',\"Jun\":'06',\"Jul\":'07',\"Aug\":'08',\"Sep\":'09',\"Oct\":10,\"Nov\":'11',\"Dec\":'12'}\n",
    "        month = dic[date[len(date)-8:len(date)-5:1]]\n",
    "        if len(date)==13:\n",
    "            day = date[0:2:1]\n",
    "        else:\n",
    "           date = '0' + date[0]\n",
    "           day = date[0:2:1]\n",
    "        return year+\"-\"+str(month)+\"-\"+day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month_dict = {\n",
    "            \"Jan\" : \"01\", \n",
    "            \"Feb\" : \"02\", \n",
    "            \"Mar\" : \"03\", \n",
    "            \"Apr\" : \"04\", \n",
    "            \"May\" : \"05\", \n",
    "            \"Jun\" : \"06\", \n",
    "            \"Jul\" : \"07\", \n",
    "            \"Aug\" : \"08\", \n",
    "            \"Sep\" : \"09\", \n",
    "            \"Oct\" : \"10\", \n",
    "            \"Nov\" : \"11\", \n",
    "            \"Dec\" : \"12\"}\n",
    "        data = date.split(\" \")\n",
    "        day = data[0][:len(data[0]) - 2]\n",
    "        if len(day) == 1:\n",
    "            day = \"0\" + day\n",
    "        month = data[1]\n",
    "        year = data[2]\n",
    "        return year+\"-\"+month_dict[month]+\"-\"+day\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        lst = date.split(\" \")\n",
    "        res = \"\"\n",
    "        dic_month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n",
    "        lst[0] = lst[0][:-2].zfill(2)\n",
    "        lst[1] = dic_month.get(lst[1])\n",
    "        return \"-\".join(lst[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\",\n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "\n",
    "        data = date.split()\n",
    "        data[0] = data[0][:-2].zfill(2)\n",
    "        data[1] = s2month[data[1]]\n",
    "        data.reverse()\n",
    "        return '-'.join(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\",\n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n",
    "        lst = date.split(\" \")\n",
    "        ans = \"%s-%s-%02d\" % (lst[2], month[lst[1]], int(lst[0][:-2]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        year = date[len(date)-4:len(date):1]\n",
    "        dic={\"Jan\":'01',\"Feb\":'02',\"Mar\":'03',\"Apr\":'04',\"May\":'05',\"Jun\":'06',\"Jul\":'07',\"Aug\":'08',\"Sep\":'09',\"Oct\":10,\"Nov\":'11',\"Dec\":'12'}\n",
    "        month = dic[date[len(date)-8:len(date)-5:1]]\n",
    "        if len(date)==13:\n",
    "            day = date[0:2:1]\n",
    "        else:\n",
    "           date = '0' + date[0]\n",
    "           day = date[0:2:1]\n",
    "        return year+\"-\"+str(month)+\"-\"+day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        m={\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        date=date.split()\n",
    "        res=[]\n",
    "        res.append(date[2])\n",
    "        res.append(m[date[1]])\n",
    "        if len(date[0])==3:\n",
    "            res.append(\"0\"+date[0][0])\n",
    "        else:\n",
    "            res.append(date[0][:2])\n",
    "\n",
    "        return \"-\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][: -2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        x = \"\"\n",
    "        for i in range (len(date)):\n",
    "            if date[i].isdigit():\n",
    "                x += date[i]\n",
    "            else:\n",
    "                break\n",
    "        if len(x) == 1:\n",
    "            x = \"0\" + x\n",
    "        return date[-4:] + \"-\" + month[date[-8:-5]] + \"-\" + x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][: -2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ddate=date.split()\n",
    "        mmon={\"Jan\":'01', \"Feb\":'02', \"Mar\":'03', \"Apr\":'04', \"May\":'05', \"Jun\":'06', \"Jul\":'07', \"Aug\":'08', \"Sep\":'09', \"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        t=[x for x in ddate[0] if x.isdigit() ]        \n",
    "        ddate[0]='{:02d}'.format(int(''.join(t)))\n",
    "        ddate[1]=mmon[ddate[1]]\n",
    "        print(ddate)\n",
    "        return ddate[2]+'-'+ddate[1]+'-'+ddate[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month, year = date.split(\" \")\n",
    "        month_dict = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        day_str = day[:-2]\n",
    "        if len(day_str) == 1:\n",
    "            day_str = \"0\"+day_str\n",
    "        return f\"{year}-{month_dict.get(month)}-{day_str}\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\",         \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\",\"Dec\":\"12\"\n",
    "        }\n",
    "        date = date.split(\" \")\n",
    "        date[0] = date[0][:-2].zfill(2)\n",
    "# 在Python中，zfill函数是一个用于字符串操作的内置函数。它用于在字符串的左侧填充指定数量的零（zero），使字符串达到指定的长度。\n",
    "#1.2 zfill函数的语法\n",
    "#str.zfill(width)\n",
    "#其中，str表示需要进行填充的字符串，width表示最终字符串的总长度。\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        return \"-\".join(date)\n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date):\n",
    "        mon = {\"Jan\": '01', \"Feb\": '02', \"Mar\": '03', \"Apr\": '04', \"May\": '05', \"Jun\": '06',\n",
    "               \"Jul\": '07', \"Aug\": '08', \"Sep\": '09', \"Oct\": '10', \"Nov\": '11', \"Dec\": '12'}\n",
    "        date_list = date.split(' ')\n",
    "        date = date_list[0][:-2]\n",
    "        if len(date) == 1:\n",
    "            date = '0' + date\n",
    "        res = date_list[2]+'-'+mon[date_list[1]]+'-'+date\n",
    "        return res\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day, mon, year = date.split()\n",
    "        mon_lst = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        mon_map = {mon:idx for idx,mon in enumerate(mon_lst,1)}\n",
    "        return \"%s-%02d-%02d\" % (year, mon_map[mon], int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ans = date.split(\" \")\n",
    "        \n",
    "        Month={\n",
    "            \"Jan\": \"01\",\n",
    "            \"Feb\": \"02\",\n",
    "            \"Mar\": \"03\",\n",
    "            \"Apr\":\"04\",\n",
    "            \"May\":\"05\",\n",
    "            \"Jun\":\"06\",\n",
    "            \"Jul\":\"07\",\n",
    "            \"Aug\":\"08\",\n",
    "            \"Sep\":\"09\",\n",
    "            \"Oct\":\"10\",\n",
    "            \"Nov\":\"11\",\n",
    "            \"Dec\":\"12\"\n",
    "        }\n",
    "        ans[2] = ans[2] + \"-\"+Month[ans[1]]\n",
    "        if len(ans[0]) == 3:\n",
    "            ans[2] = ans[2] + \"-0\"+ans[0][0:1]\n",
    "        else:\n",
    "            ans[2] = ans[2] + \"-\"+ans[0][0:2]\n",
    "        return ans[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date_li = date.split()\n",
    "        months_set = [\"\",\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        days_set = [\"\", \"1st\", \"2nd\", \"3rd\", \"4th\", \"5th\", \"6th\", \"7th\", \"8th\", \"9th\", \"10th\", \"11th\", \"12th\", \"13th\", \"14th\", \"15th\", \"16th\", \"17th\", \"18th\", \"19th\", \"20th\", \"21st\", \"22nd\", \"23rd\", \"24th\", \"25th\", \"26th\", \"27th\", \"28th\", \"29th\", \"30th\", \"31st\"]\n",
    "        year, month, day = date_li[-1], str(months_set.index(date_li[1])), str(days_set.index(date_li[0]))\n",
    "        day = '0' + day if len(day)==1 else day\n",
    "        month = '0' + month if len(month)==1 else month\n",
    "        return year + '-' + month + '-' + day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][: -2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month_map = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        day, month, year = date.split(' ')\n",
    "        day = day[:-2]\n",
    "        if len(day) < 2:\n",
    "            day = '0' + day\n",
    "        month = str(month_map.index(month) + 1)\n",
    "        if len(month) < 2:\n",
    "            month = '0' + month\n",
    "        return year + '-' + month + '-' + day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        ans=''\n",
    "        date=date.split()\n",
    "        ans+=str(date[2])\n",
    "        ans+='-'\n",
    "        month=['',\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        m=month.index(date[1])\n",
    "        if m<10:\n",
    "            ans+='0'+str(m)+'-'\n",
    "        else:\n",
    "            ans+=str(m)+'-'\n",
    "        if len(date[0])==3:\n",
    "            ans+='0'+date[0][0]\n",
    "        else:\n",
    "            ans+=date[0][:2]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        month = {\"Jan\": \"01\",\n",
    "                 \"Feb\": \"02\",\n",
    "                 \"Mar\": \"03\",\n",
    "                 \"Apr\": \"04\",\n",
    "                 \"May\": \"05\", \n",
    "                 \"Jun\": \"06\",\n",
    "                 \"Jul\": \"07\", \n",
    "                 \"Aug\": \"08\", \n",
    "                 \"Sep\": \"09\", \n",
    "                 \"Oct\": \"10\", \n",
    "                 \"Nov\": \"11\", \n",
    "                 \"Dec\": \"12\"\n",
    "                 }\n",
    "        parse = date.split(\" \")\n",
    "        ans = parse[2]+\"-\"+month[parse[1]]+\"-\"\n",
    "        if len(parse[0]) == 3:\n",
    "            ans += \"0\"+parse[0][0]\n",
    "        else:\n",
    "            ans += parse[0][0:2]\n",
    "        return ans         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][:-2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        s2month = {\n",
    "            \"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n",
    "            \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"\n",
    "        }\n",
    "        \n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        date[0] = date[0][: -2].zfill(2)\n",
    "        date[1] = s2month.get(date[1])\n",
    "        date.reverse()\n",
    "        \n",
    "        return \"-\".join(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        months={\"Jan\":'01',\"Feb\":'02',\"Mar\":'03',\"Apr\":'04',\"May\":'05',\"Jun\":'06',\"Jul\":'07',\"Aug\":'08',\"Sep\":'09',\"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        time=date.split()\n",
    "        if len(time[0])==3:\n",
    "            time[0]='0'+time[0]\n",
    "        return time[-1]+'-'+months[time[-2]]+'-'+time[0][0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from datetime import datetime\n",
    "import re\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        return datetime.strptime(re.sub('st|nd|rd|th', '', date), '%d %b %Y').strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        d = {\"Jan\":'01', \"Feb\":'02', \"Mar\":'03', \"Apr\":'04', \"May\":'05', \"Jun\":'06', \"Jul\":'07', \"Aug\":'08', \"Sep\":'09', \"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        date = date.split()\n",
    "        ans = [date[-1]]\n",
    "        ans.append(d[date[-2]])\n",
    "        if len(date[0])==4:\n",
    "            ans.append(date[0][:2])\n",
    "        else:\n",
    "            ans.append(\"0\"+date[0][0])\n",
    "        return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month,year=date.split()\n",
    "        li=[\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        dic={mon:id for id,mon in enumerate(li,1)}\n",
    "        return \"%s-%02d-%02d\" %(year,dic[month],int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date_li = date.split()\n",
    "        months_set = [\"\",\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        days_set = [\"\", \"1st\", \"2nd\", \"3rd\", \"4th\", \"5th\", \"6th\", \"7th\", \"8th\", \"9th\", \"10th\", \"11th\", \"12th\", \"13th\", \"14th\", \"15th\", \"16th\", \"17th\", \"18th\", \"19th\", \"20th\", \"21st\", \"22nd\", \"23rd\", \"24th\", \"25th\", \"26th\", \"27th\", \"28th\", \"29th\", \"30th\", \"31st\"]\n",
    "        year, month, day = date_li[-1], str(months_set.index(date_li[1])), str(days_set.index(date_li[0]))\n",
    "        day = '0' + day if len(day)==1 else day\n",
    "        month = '0' + month if len(month)==1 else month\n",
    "        return year + '-' + month + '-' + day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date = date.split(\" \")\n",
    "        \n",
    "        day = \"0\" + date[0][:1] if len(date[0]) == 3 else date[0][:2]\n",
    "        m = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\",\"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        mon = m[date[1]]\n",
    "        year = date[2]\n",
    "        return \"-\".join([year, mon, day])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        segs = list(re.search(r'(\\d{1,2}).*?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*?(\\d{4})', date, flags=re.I).groups())\n",
    "        segs[1] = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"].index(segs[1]) + 1\n",
    "        day, month, year = segs\n",
    "        return datetime(int(year), month, int(day)).strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        my_dict = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\", \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        date1 = date.split()\n",
    "        res = \"\"\n",
    "        res = res+date[-4:]+\"-\"+my_dict[date1[1]]+\"-\"\n",
    "        if date[1].isalpha():\n",
    "            date = '0'+date\n",
    "        for i in range(len(date)):\n",
    "            if date[i].isdigit():\n",
    "                res = res+date[i]\n",
    "            else:\n",
    "                break\n",
    "        return res\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        po = date.split(' ')\n",
    "        months = {}\n",
    "        moth = []\n",
    "        for i in range(1,32):\n",
    "            moth.append(i)\n",
    "        moth_2 =  [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        for a,b in zip(moth,moth_2):\n",
    "            months[b] = a\n",
    "        month = months[po[1]]   #月份\n",
    "        if len(str(month)) == 1:\n",
    "            month = '0' + str(month)\n",
    "        if len(po[0]) == 3:      #日\n",
    "            day = '0'+po[0][0]\n",
    "        if len(po[0]) == 4:\n",
    "            day = po[0][:2]\n",
    "        year = po[2]\n",
    "        ik = '{}-{}-{}'.format(year,month,day)\n",
    "        return ik"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day, mon, year = date.split()\n",
    "        mon_lst = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']\n",
    "        mon_map = {mon:idx for idx, mon in enumerate(mon_lst, 1)}\n",
    "        return '%s-%02d-%02d' % (year, mon_map[mon], int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from datetime import datetime\n",
    "\n",
    "months = (\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\")\n",
    "months_regex = '|'.join(months)\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        segs = re.search(rf'(\\d{{1,2}}).*?({months_regex}).*?(\\d{{4}})', date, flags=re.I).groups()\n",
    "        day, month, year = segs\n",
    "        month = months.index(month) + 1\n",
    "        return datetime(int(year), month, int(day)).strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day,month,year=date.split()\n",
    "        dic={\"Jan\":1, \"Feb\":2, \"Mar\":3, \"Apr\":4, \"May\":5, \"Jun\":6, \"Jul\":7, \"Aug\":8, \"Sep\":9, \"Oct\":10, \"Nov\":11, \"Dec\":12}\n",
    "        m=dic.get(month)\n",
    "        return \"%s-%02d-%02d\" %(year,m,int(day[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        months = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\",\n",
    "         \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "\n",
    "        pattern = '([0-9]{1,2})[a-z]{2}[ \\t]+(\\w{3})[ \\t]+(\\d{4})'\n",
    "        day = re.search(pattern,date).group(1)\n",
    "        if len(day) == 1:\n",
    "            day = '0' + day\n",
    "        month = re.search(pattern,date).group(2)\n",
    "        year = re.search(pattern,date).group(3)\n",
    "    \n",
    "        return year +\"-\"+ months[month] +\"-\"+ day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        months = {\"Jan\":\"01\", \"Feb\":\"02\", \"Mar\":\"03\", \"Apr\":\"04\",\n",
    "         \"May\":\"05\", \"Jun\":\"06\", \"Jul\":\"07\", \"Aug\":\"08\", \"Sep\":\"09\", \"Oct\":\"10\", \"Nov\":\"11\", \"Dec\":\"12\"}\n",
    "        pattern = '([0-9]{1,2})[a-z]{2}[ \\t]+(\\w{3})[ \\t]+(\\d{4})'\n",
    "        day = re.search(pattern,date).group(1)\n",
    "        if len(day) == 1:\n",
    "            day = '0' + day\n",
    "        month = re.search(pattern,date).group(2)\n",
    "        year = re.search(pattern,date).group(3)\n",
    "        return year +\"-\"+ months[month] +\"-\"+ day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        date_list = date.split(' ')\n",
    "        res = ''\n",
    "        d = {\"Jan\":'01', \"Feb\":'02', \"Mar\":'03', \"Apr\":'04', \"May\":'05', \"Jun\":'06', \"Jul\":'07', \"Aug\":'08', \"Sep\":'09', \"Oct\":'10', \"Nov\":'11', \"Dec\":'12'}\n",
    "        res += date_list[2]\n",
    "        res += '-'\n",
    "        res += d[date_list[1]]\n",
    "        res += '-'\n",
    "        t = date_list[0][:len(date_list[0])-2]\n",
    "        if len(t) == 1:\n",
    "            t = '0'+t\n",
    "        res += t\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        day, mon, year = date.split()\n",
    "        mon_lst = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        mon_map = {mon:idx for idx,mon in enumerate(mon_lst,1)}\n",
    "        return \"%s-%02d-%02d\" % (year, mon_map[mon], int(day[:-2]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        dd, mm, yy = date.split(\" \")\n",
    "        dd = \"\".join(w for w in dd if w.isnumeric())\n",
    "        lst = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n",
    "        mm = str(lst.index(mm) + 1)\n",
    "        if len(dd) == 1:\n",
    "            dd = \"0\" + dd\n",
    "        if len(mm) == 1:\n",
    "            mm = \"0\" + mm\n",
    "        return f\"{yy}-{mm}-{dd}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        y = dict(zip((\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"),(1,2,3,4,5,6,7,8,9,10,11,12)))\n",
    "        x = date[0:2] if len(date) == 13 else \"0\"+date[0]\n",
    "        return f\"{date[-4:]}-{y[date[-8:-5]]:0>2d}-{x}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from datetime import datetime\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        segs = re.search(r'(\\d{1,2}).*?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*?(\\d{4})', date, flags=re.I).groups()\n",
    "        day, month, year = segs\n",
    "        month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"].index(month) + 1\n",
    "        return datetime(int(year), month, int(day)).strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatDate(self, date: str) -> str:\n",
    "        dic = {\"Jan\":\"01\", \n",
    "                \"Feb\":\"02\",\n",
    "                \"Mar\":\"03\",\n",
    "                \"Apr\":\"04\",\n",
    "                \"May\":\"05\",\n",
    "                \"Jun\":\"06\",\n",
    "                \"Jul\":\"07\",\n",
    "                \"Aug\":\"08\",\n",
    "                \"Sep\":\"09\",\n",
    "                \"Oct\":\"10\",\n",
    "                \"Nov\":\"11\",\n",
    "                \"Dec\":\"12\"}\n",
    "        date = date.split(\" \")\n",
    "        if int(date[0][:-2])<10:\n",
    "            day = \"0\"+ date[0][:-2]\n",
    "        else:\n",
    "            day = date[0][:-2]\n",
    "        return date[2]+\"-\"+dic[date[1]]+\"-\"+day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join([x for x in number if x.isdigit()])\n",
    "        idx,l,ans = 0,len(s),[]\n",
    "        while True:\n",
    "            if l - idx > 4:\n",
    "                ans.append(s[idx:idx+3])\n",
    "                idx += 3\n",
    "            else:\n",
    "                if l - idx == 4:\n",
    "                    ans.append(s[idx:idx+2])\n",
    "                    ans.append(s[idx+2:])\n",
    "                else:\n",
    "                    ans.append(s[idx:])\n",
    "                return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        n,pt = len(digits),0\n",
    "        ans = list()\n",
    "\n",
    "        while n>0:\n",
    "            if n>4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt +=3\n",
    "                n -=3\n",
    "            else:\n",
    "                if n==4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = \"\".join((\"\".join(number.split())).split('-'))\n",
    "        print(number)\n",
    "        l = len(number)\n",
    "        if l <= 3: return number\n",
    "        tail = \"\"\n",
    "        if l % 3:\n",
    "            if l % 3 == 1:\n",
    "                tail = number[-4:-2] + \"-\" + number[-2:]\n",
    "                number = number[:-4]\n",
    "            else:\n",
    "                tail = number[-2:]\n",
    "                number = number[:-2]\n",
    "                print(\"tail\", tail, \"number\", number)\n",
    "        ans = \"\"\n",
    "        for i in range(0, len(number), 3):\n",
    "            ans += number[i : i + 3] + \"-\"\n",
    "        return ans + tail if tail != \"\" else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\",\"\")\n",
    "        limit = len(number)\n",
    "        n = limit%3\n",
    "        ans = [number[i:i+3] for i in range(0,limit-2,3)]\n",
    "        if n == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        if n == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        a=\"\"\n",
    "        n=len(number)\n",
    "        an=[number[i*3:i*3+3]\n",
    "        for i in range(n//3)]\n",
    "        if n%3==1:\n",
    "            an[-1]=an[-1][:2]\n",
    "            an.append(number[-2:])\n",
    "        if n%3==2:\n",
    "            an.append(number[-2:])\n",
    "        for i in range(len(an)):\n",
    "            a+=(an[i] + \"-\")\n",
    "        return a[0:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        str1 = ''\n",
    "        component = []\n",
    "        for i in number:\n",
    "            if i.isdigit():\n",
    "                str1 += i                \n",
    "        while (len(str1)) > 4:\n",
    "            component.append(str1[:3])\n",
    "            str1 = str1[3:]\n",
    "        if (len(str1)) == 4:\n",
    "            component.append(str1[:2])\n",
    "            component.append(str1[2:])\n",
    "        else:\n",
    "            component.append(str1[:])\n",
    "        return '-'.join(component)\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n, idx = len(s), 0\n",
    "        res = \"\"\n",
    "        while idx < n:\n",
    "            if res:\n",
    "                res += \"-\"\n",
    "            if idx + 5 > n:\n",
    "                res += s[idx:] if idx + 3 >= n else f\"{s[idx:idx+2]}-{s[idx+2:]}\"\n",
    "                break\n",
    "            res += s[idx:idx+3]\n",
    "            idx += 3\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number= number.replace(\"-\",\"\").replace(\" \",\"\")\n",
    "        s= \"\"\n",
    "        while(True):\n",
    "            n = len(number)\n",
    "            if n <=3:\n",
    "                return s+number\n",
    "            elif n==4:\n",
    "                return s+number[:2]+'-'+number[2:]\n",
    "            else:\n",
    "                s = s + number[:3]+\"-\"\n",
    "                number = number[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace('-','').replace(' ','')\n",
    "        ans = [number[i : i + 3] for i in range(0, len(number), 3)]\n",
    "        if len(ans[-1]) == 1:\n",
    "            ans[-1] = ans[-2][-1] + ans[-1]\n",
    "            ans[-2] = ans[-2][:2]\n",
    "        return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        clean_str = re.sub(' |-', '', number)\n",
    "        \n",
    "        n = len(clean_str)//3\n",
    "        remainder = len(clean_str)%3\n",
    "        ans = []\n",
    "        for i in range(n-1):\n",
    "            ans.append(clean_str[3*i:3*(i+1)])\n",
    "        if remainder == 1:\n",
    "            ans.append(clean_str[-4:-2])\n",
    "            ans.append(clean_str[-2:])\n",
    "            \n",
    "        elif remainder == 2:\n",
    "            if n:\n",
    "                ans.append(clean_str[-5:-2])\n",
    "                ans.append(clean_str[-2:])\n",
    "            else:\n",
    "                ans.append(clean_str[-2:])\n",
    "        else:\n",
    "            ans.append(clean_str[-3:])\n",
    "        \n",
    "        # ans = [i for i in ans if i]\n",
    "        return '-'.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\")\n",
    "        number = number.replace(\"-\", \"\")\n",
    "        num_len = len(number)\n",
    "        res = []\n",
    "        for index, s in enumerate(number):\n",
    "            if index % 3 == 0 and index != 0:\n",
    "                tmp = number[index-3: index]\n",
    "                res.append(tmp)\n",
    "        res.append(number[(index//3)*3:])\n",
    "        if len(res[-1]) == 1:\n",
    "            res[-1] = res[-2][-1] + res[-1]\n",
    "            res[-2] = res[-2][:-1]\n",
    "        \n",
    "        result = \"\"\n",
    "        for i in res:\n",
    "            result += i + \"-\"\n",
    "        return result[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = re.sub(r'[-\\s]', '', number)\n",
    "        ans = ''\n",
    "        m = len(number)\n",
    "        n = 0\n",
    "        while n < m:\n",
    "            if m-n == 4:\n",
    "                ans += number[n:n+2] + '-' + number[n + 2:n + 4]\n",
    "                break\n",
    "            elif m-n == 2:\n",
    "                ans += number[n:n+2]\n",
    "                break\n",
    "            elif m - n == 3:\n",
    "                ans += number[n:n+3]\n",
    "                break\n",
    "            else:\n",
    "                ans += number[n:n+3] + '-'\n",
    "                n+= 3\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace(\"-\",\"\").replace(\" \",\"\")\n",
    "        n=len(number)\n",
    "        ans=[number[3*i:3*i+3] for i in range(n//3)]\n",
    "        if n%3==1:\n",
    "            ans[-1]=ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n%3==2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','')\n",
    "        number = number.replace('-','')\n",
    "        print(number)\n",
    "        ans = []\n",
    "        m = len(number)\n",
    "        n = 0\n",
    "        while n < m:\n",
    "            if m-n == 4:\n",
    "                ans.append(number[n:n+2])\n",
    "                n += 2\n",
    "                ans.append(number[n:n+2]) \n",
    "                n= m\n",
    "            elif m-n == 2:\n",
    "                ans.append(number[n:n+2]) \n",
    "                n = m\n",
    "            else:\n",
    "                ans.append(number[n:n+3])\n",
    "                n+= 3\n",
    "        print(ans)\n",
    "        return '-'.join(ans) \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','').replace(' ','')\n",
    "        print(number)\n",
    "        arr=str(number)\n",
    "        n=len(arr)\n",
    "        # ans = [number[i * 3: i * 3 + 3] for i in range(n // 3)]\n",
    "        res=[]\n",
    "        while len(number)>4:\n",
    "            res.append(number[:3])\n",
    "            number=number[3:]\n",
    "        if len(number)==4:\n",
    "            res.append(number[:2])\n",
    "            res.append(number[2:])\n",
    "        else:\n",
    "            res.append(number[:])\n",
    "        return '-'.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        result = []\n",
    "        count = 0\n",
    "        number = number.replace(\"-\",\"\").replace(\" \",\"\") #去除空格和破折号\n",
    "        for c in number:\n",
    "            count+=1\n",
    "            result.append(c)\n",
    "            if count % 3 == 0:\n",
    "                result.append('-')\n",
    "        if count % 3 == 0:\n",
    "            result.pop()\n",
    "        elif count % 3 == 1:\n",
    "            c = result[-3]\n",
    "            result[-3] = '-'\n",
    "            result[-2] = c\n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        a =\"\"\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] \n",
    "        for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        if n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        for i in range(len(ans)):\n",
    "            a+=(ans[i] + \"-\")\n",
    "        return a[0:-1]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ', '').replace('-', '')\n",
    "        n = len(number)\n",
    "        res = [number[3*i:3*(i+1)] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            res[-1] = res[-1][:2]\n",
    "            res.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            res.append(number[-2:])\n",
    "        \n",
    "        return '-'.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join(ch for ch in number if ch.isalnum())\n",
    "        i = 0\n",
    "        res = ''\n",
    "        while len(s[i:]) > 4:\n",
    "            res = res + s[i:i+3] +'-'\n",
    "            i += 3\n",
    "    \n",
    "        if len(s[i:]) == 4:\n",
    "            res = res + s[i:i+2] +'-'\n",
    "            res += s[i+2:]\n",
    "        else:\n",
    "            res += s[i:]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 先去除空格和破折号\n",
    "        numbers = number.replace('-', '').replace(' ', '')\n",
    "        l = len(numbers)\n",
    "        \n",
    "        def C(con, num):\n",
    "            lg = len(num)\n",
    "            ls = ''\n",
    "            count = 0\n",
    "            for i in range(lg):\n",
    "                count += 1\n",
    "                ls += num[i]\n",
    "                if count == con and i != lg - 1:\n",
    "                    ls += '-'\n",
    "                    count = 0\n",
    "            return ls\n",
    "        # 根据number的长度来判断选用\n",
    "        if l <= 3:\n",
    "            return numbers\n",
    "        if l % 3 == 0:\n",
    "            return C(3, numbers)\n",
    "        elif l % 3 == 1:\n",
    "            if l==4:\n",
    "                return C(2, numbers[l - 4:])\n",
    "            else:\n",
    "                return C(3, numbers[0:l - 4]) +'-'+ C(2, numbers[l - 4:])\n",
    "        elif l % 3 == 2:\n",
    "            return C(3, numbers[0:l - 2] + C(2, numbers[l - 2:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        res, tmp = '', 0\n",
    "        ls = list(number)\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i] == '-' or ls[i] == ' ':\n",
    "                ls[i] = ''\n",
    "            else:\n",
    "                tmp += 1\n",
    "                res += ls[i]\n",
    "                if tmp == 3:\n",
    "                    res += '-'\n",
    "                    tmp = 0\n",
    "        if tmp == 0:\n",
    "            res = res[:-1]\n",
    "        elif tmp == 1:\n",
    "            res = res[:-3] + '-' + res[-3] + res[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        res, tmp = '', 0\n",
    "        ls = list(number)\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i] == '-' or ls[i] == ' ':\n",
    "                ls[i] = ''\n",
    "            else:\n",
    "                tmp += 1\n",
    "                res += ls[i]\n",
    "                if tmp == 3:\n",
    "                    res += '-'\n",
    "                    tmp = 0\n",
    "        if tmp == 0:\n",
    "            res = res[:-1]\n",
    "        elif tmp == 1:\n",
    "            res = res[:-3] + '-' + res[-3] + res[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c.isdigit())  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c.isdigit())  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "\n",
    "\n",
    "\n",
    "        newNum = \"\"\n",
    "\n",
    "\n",
    "        for num in number:\n",
    "\n",
    "\n",
    "            if num.isdigit():\n",
    "                newNum += num\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        res = \"\"\n",
    "\n",
    "        n = len(newNum)\n",
    "        print(newNum)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            left = n - i \n",
    "            if left > 4:\n",
    "                res += newNum[i: i + 3] + \"-\"\n",
    "            else:\n",
    "                break\n",
    "            i += 3\n",
    "        print(left)\n",
    "        if left == 2 or left == 3:\n",
    "            res += newNum[i:]\n",
    "        elif left == 4:\n",
    "            res +=  newNum[i: i + 2] + \"-\" + newNum[i + 2:]\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# code 1\n",
    "# import re\n",
    "# class Solution:\n",
    "#     def reformatNumber(self, number: str) -> str:\n",
    "#         self.number = number\n",
    "#         num = str(re.sub(' |-', '',self.number))\n",
    "#         lst = []\n",
    "\n",
    "#         while len(num) > 4:\n",
    "#             lst.append(num[0:3])\n",
    "#             num = num[3:]\n",
    "#         if len(num) == 4:\n",
    "#             lst.append(num[0:2])\n",
    "#             lst.append(num[2:])\n",
    "#         else:\n",
    "#             lst.append(num)\n",
    "        \n",
    "#         result = '-'.join(lst)\n",
    "#         return result\n",
    "\n",
    "\n",
    "# code 2\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for char in number:\n",
    "            if char.isdigit():\n",
    "                digits.append(char)\n",
    "                \n",
    "        n,pt = len(digits),0\n",
    "        ans = list()\n",
    "\n",
    "        while n>0:\n",
    "            if n>4:\n",
    "                ans.append(''.join(digits[pt:pt+3]))\n",
    "                pt+=3\n",
    "                n-=3\n",
    "            else:\n",
    "                if n==4:\n",
    "                    ans.append(''.join(digits[pt:pt+2]))\n",
    "                    ans.append(''.join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(''.join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        arr=[]\n",
    "        for i in number:\n",
    "            if i!=' ' and i!='-':\n",
    "                arr.append(i)\n",
    "        n=len(arr)\n",
    "        ans=''\n",
    "        p=''\n",
    "        if n%3==1:\n",
    "            p=arr[-4]+arr[-3]+'-'+arr[-2]+arr[-1]+'-'\n",
    "            n-=4\n",
    "        elif n%3==2:\n",
    "            p=arr[-2]+arr[-1]+'-'\n",
    "            n-=2\n",
    "        i=0\n",
    "        while i<n:\n",
    "            ans+=arr[i]+arr[i+1]+arr[i+2]\n",
    "            ans+='-'\n",
    "            i+=3\n",
    "        ans+=p\n",
    "        return ans[:-1]\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        pos = 0\n",
    "        total = sum(1 for x in number if x !='-' and x !=' ')\n",
    "        need = 0\n",
    "        if total % 3 ==0:\n",
    "            need = total-3\n",
    "        else:\n",
    "            need = total-2\n",
    "        for i in range(len(number)):\n",
    "            if number[i] != '-' and number[i] !=' ':\n",
    "                ans+=number[i]\n",
    "                pos+=1\n",
    "                if (pos<need and pos % 3==0) or pos ==need:\n",
    "                    ans += '-'\n",
    "        \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        lst = [x for x in number if x.isnumeric()]\n",
    "        ans = []\n",
    "        while len(lst) > 4:\n",
    "            ans.append(\"\".join(lst[:3]))\n",
    "            lst = lst[3:]\n",
    "        if len(lst) <= 3:\n",
    "            ans.append(\"\".join(lst))\n",
    "        else:\n",
    "            ans.append(\"\".join(lst[:2]))\n",
    "            ans.append(\"\".join(lst[2:]))\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join([x for x in number if x.isdigit()])\n",
    "        idx,l,ans = 0,len(s),[]\n",
    "        while True:\n",
    "            if l - idx > 4:\n",
    "                ans.append(s[idx:idx+3])\n",
    "                idx += 3\n",
    "            else:\n",
    "                if l - idx == 4:\n",
    "                    ans.append(s[idx:idx+2])\n",
    "                    ans.append(s[idx+2:])\n",
    "                else:\n",
    "                    ans.append(s[idx:])\n",
    "                return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        nstr = number.replace(' ', '').replace('-', '')\n",
    "        n = len(nstr)\n",
    "        ans = ''\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if n - i > 4:\n",
    "                ans += nstr[i: i+3] + '-'\n",
    "                i += 3\n",
    "            if n - i == 4:\n",
    "                ans += nstr[i: i+2] + '-' + nstr[i + 2:]\n",
    "                break\n",
    "            if n - i <=3:\n",
    "                ans += nstr[i:]\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        for i in range(len(number)):\n",
    "            if number[i].isdigit():\n",
    "                ans += number[i]\n",
    "        n = len(ans)\n",
    "        if n<=3:return ans\n",
    "        m , residual = (n-4) //3 ,(n-4) % 3\n",
    "        res = ''\n",
    "        for i in range(m):\n",
    "            res = res + ans[i*3 : (i+1)*3] + '-'\n",
    "        if residual == 0:\n",
    "            res = res + ans[-4:-2] + '-' + ans[-2:]\n",
    "        elif residual == 1:\n",
    "            res = res + ans[-5:-2] + '-' + ans[-2:]\n",
    "        else:\n",
    "            res = res + ans[-6:-3] + '-' + ans[-3:]\n",
    "        return res\n",
    "        \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")#进行删除处理\n",
    "        s = \"\"\n",
    "        for x in range(0,len(number),3):\n",
    "            if x == len(number) - 4: #考虑最后出现剩余4个数字的情况\n",
    "                s += number[x:x+2] + \"-\" + number[x+2:]\n",
    "                break\n",
    "            elif x == len(number) - 2:\n",
    "                s += number[x:x+2]\n",
    "                break\n",
    "            else:\n",
    "                s += number[x:x+3] + \"-\" #进行分组处理\n",
    "        if s[-1] == \"-\" :\n",
    "            return s[:-1] #解决最后末尾出现的\"-\"\n",
    "        else:\n",
    "            return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        \n",
    "        temp = []\n",
    "        for c in number:\n",
    "            if c in \"0123456789\":\n",
    "                temp.append(c)\n",
    "                \n",
    "        #n = len(temp)\n",
    "        ans = \"\"\n",
    "        count = 0\n",
    "        while temp:\n",
    "            if len(temp) == 4:\n",
    "                ans += temp[0]\n",
    "                ans += temp[1]\n",
    "                ans += '-'\n",
    "                ans += temp[2]\n",
    "                ans += temp[3]\n",
    "                break\n",
    "            elif len(temp) < 4:\n",
    "                for i in temp:\n",
    "                    ans += i\n",
    "                break\n",
    "            c = \"\"\n",
    "            for i in range(3):\n",
    "                try:\n",
    "                    c += temp.pop(0)\n",
    "                    count += 1\n",
    "                except:\n",
    "                    break\n",
    "            ans += c\n",
    "            if count % 3 == 0:\n",
    "                ans += '-'\n",
    "        return ans\n",
    "            \n",
    "        '''\n",
    "        res = \"\"\n",
    "        number1 = \"\"\n",
    "        count = 0\n",
    "        while count < len(number):\n",
    "            if number[count] != \" \" and number[count] != \"-\":\n",
    "                number1 += number[count]\n",
    "            count += 1\n",
    "        count = 0\n",
    "        while count < len(number1):\n",
    "            if count + 1 % 3 == 0 and count != 0:\n",
    "                res += \"-\"\n",
    "            res += number1[count]\n",
    "            count += 1\n",
    "            if len(number1) - count <= 4:\n",
    "                if len(number1) - count == 2:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                if len(number1) - count == 3:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-3]\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                if len(number1) - count == 4:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-4]\n",
    "                    res += number1[-3]\n",
    "                    res += \"-\"\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                break\n",
    "        return res\n",
    "        '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        nums = ''.join(''.join(number.split()).split('-'))\n",
    "        #print(nums)\n",
    "        i = 0\n",
    "        n = len(nums)\n",
    "        res = ''\n",
    "        while i < n - 1 and i + 4 < n:\n",
    "            res = res + nums[i: i + 3] + '-'\n",
    "            i += 3\n",
    "        print(res)\n",
    "        m = len(nums[i:])\n",
    "        if m == 2 or m == 3:\n",
    "            res += nums[i:]\n",
    "        else:\n",
    "            res = res + nums[i: i + 2] + '-' + nums[i + 2:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        fh = []\n",
    "        for i in number:\n",
    "            if i.isdigit():\n",
    "                fh.append(i)\n",
    "        n = len(fh)\n",
    "        if n%3 == 1:\n",
    "            if n == 4:\n",
    "                return \"\".join(fh[-4:-2]) + \"-\" + \"\".join(fh[-2:])\n",
    "            return \"-\".join(\"\".join(fh[i:i+3]) for i in range(0, n-4,3)) + \"-\" + \"\".join(fh[-4:-2]) + \"-\" + \"\".join(fh[-2:])\n",
    "        else:\n",
    "            return \"-\".join(\"\".join(fh[i:i+3]) for i in range(0, n, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n = len(number)\n",
    "        if n % 3 == 1:\n",
    "            m = n - 4\n",
    "            s = number[m: m + 2] + \"-\" + number[m + 2:]\n",
    "        else:\n",
    "            m = n - n % 3\n",
    "            s = number[m:]\n",
    "        ans = \"\"\n",
    "        for i in range(0, m, 3):\n",
    "            ans += number[i: i + 3] + \"-\"\n",
    "        ans = ans + s\n",
    "        return ans if n % 3 != 0 else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        a=number.replace('-','')\n",
    "        b=a.replace(' ','')   \n",
    "        print(b)  \n",
    "        res=[]           \n",
    "        for i in range(0,len(b),3):\n",
    "            res.append(b[i:i+3])    \n",
    "        if len(res[-1])==1 and len(res)>1:\n",
    "            t=res[-2]+res[-1]\n",
    "            res[-2],res[-1]=t[:2],t[2:]\n",
    "        \n",
    "        return '-'.join(res)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace(' ','').replace('-','')\n",
    "        num=0\n",
    "        res=''\n",
    "        for i in number:\n",
    "            res+=i\n",
    "            num+=1\n",
    "            if num==3:\n",
    "                res+='-'\n",
    "                num=0\n",
    "        if res[-1]=='-':res=res[:-1]\n",
    "        if res[-2]=='-':res=res[:-3]+'-'+res[-3]+res[-1]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        temp = \"\"\n",
    "        for num in number:\n",
    "            if num != \" \" and num != \"-\":\n",
    "                temp += num\n",
    "        length = len(temp)\n",
    "        res = \"\"\n",
    "        start = 0\n",
    "        while length>4:\n",
    "            res += temp[start:start+3]+\"-\"\n",
    "            start += 3\n",
    "            length -= 3\n",
    "        if length == 4:\n",
    "            res += temp[start:start+2]+\"-\"+temp[start+2:start+4]\n",
    "        elif length == 3:\n",
    "            res += temp[start:start+3]\n",
    "        elif length == 2:\n",
    "            res += temp[start:start+2]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = re.compile(r\"[- ]\").sub(\"\", number)\n",
    "        idx, n, ans = 0, len(number), []\n",
    "        while idx + 4 < n:\n",
    "            ans.append(number[idx : idx + 3])\n",
    "            idx += 3\n",
    "        if n - idx == 4:\n",
    "            ans.append(number[idx : idx + 2])\n",
    "            idx += 2\n",
    "        ans.append(number[idx:n])\n",
    "        return \"-\".join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n = len(number)\n",
    "        if n % 3 == 1:\n",
    "            if n > 3:\n",
    "                m = n - 4\n",
    "                s = number[m: m + 2] + \"-\" + number[m + 2:]\n",
    "            else:\n",
    "                m = 1\n",
    "                s = number\n",
    "        else:\n",
    "            m = n - n % 3\n",
    "            if n % 3 != 0:\n",
    "                s = number[m:]\n",
    "            else:\n",
    "                s = \"\"\n",
    "        lst = []\n",
    "        for i in range(0, m, 3):\n",
    "            lst.append(number[i: i + 3])\n",
    "        lst.append(s)\n",
    "        ans = \"-\".join(lst)\n",
    "        return ans if n % 3 != 0 else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','').replace('-','')\n",
    "        components = []\n",
    "\n",
    "        while len(number) > 4:\n",
    "            components.append(number[:3])\n",
    "            number = number[3:]\n",
    "\n",
    "        if len(number) == 4:\n",
    "            components.append(number[:2])\n",
    "            components.append(number[2:])\n",
    "        else:\n",
    "            components.append(number[:])\n",
    "\n",
    "        return '-'.join(components)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','').replace('-','')\n",
    "        n = len(number)\n",
    "        iter = n // 3\n",
    "        if n <=3:\n",
    "            return number\n",
    "        elif n % 3 == 0:\n",
    "            str = ''\n",
    "            for i in range(iter):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str[:-1]\n",
    "            return number\n",
    "        elif n % 3 == 1:\n",
    "            str = ''\n",
    "            for i in range(iter-1):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str + number[-4:-2] + '-' + number[-2:]\n",
    "            return number\n",
    "        else:\n",
    "            str = ''\n",
    "            for i in range(iter):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str + number[-2:]\n",
    "            return number\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','')\n",
    "        number=number.replace(' ','')\n",
    "        ans=''\n",
    "        n=len(number)\n",
    "        if n%3==1:\n",
    "            n=n-4\n",
    "        for i in range(0,n,3):\n",
    "            ans+=number[i:i+3]+\"-\"\n",
    "        if len(number)!=n:\n",
    "            ans=ans+number[n:n+2]+\"-\"+number[n+2:]\n",
    "        if ans[-1]=='-':\n",
    "            ans=ans[:-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        x = \"\"\n",
    "        target = \"\"\n",
    "        for i in range (len(number)):\n",
    "            if number[i].isdigit():\n",
    "                x += number[i]\n",
    "        count = len(x)\n",
    "        while count > 4:\n",
    "            target += x[:3] + \"-\"\n",
    "            count -= 3\n",
    "            x = x[3:]\n",
    "        if count == 4:\n",
    "            return target + x[:2] + \"-\" + x[2:]\n",
    "        else:\n",
    "            return target + x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        new_number=[]\n",
    "        for i in number:\n",
    "            if i !=' 'and i!='-':\n",
    "                new_number.append(i)\n",
    "\n",
    "        length=len(new_number)\n",
    "        i=0\n",
    "\n",
    "        output_number=''\n",
    "        left=length\n",
    "        while left>4:\n",
    "            for j in range(3):\n",
    "\n",
    "                output_number+=new_number[i]\n",
    "                i+=1\n",
    "                left-=1\n",
    "            output_number+='-'\n",
    "        if left==4:\n",
    "            output_number+=new_number[-4]\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+='-'\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==3:\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==2:\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        return output_number\n",
    "\n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        tmp = list(number.replace(' ',\"\").replace(\"-\", \"\"))\n",
    "        resList = []\n",
    "        res_tmp=\"\" \n",
    "        while True:\n",
    "            try:\n",
    "                res_tmp += tmp.pop(0)\n",
    "            except Exception:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                break\n",
    "            \n",
    "            if len(res_tmp)==2 and len(tmp)==2:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                res_tmp=\"\"            \n",
    "            \n",
    "            if len(res_tmp)==3:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                res_tmp=\"\"\n",
    "        \n",
    "        \n",
    "        \n",
    "        return \"-\".join(resList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:        \n",
    "        n3 = ''                             #存储纯数字的地方\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")\n",
    "        l = len(number)                     #求字符串长度\n",
    "        m = l // 3                          #求能切几下\n",
    "        m2 = l % 3\n",
    "        if l > 3:\n",
    "            for j in range(0, m-1):\n",
    "                n3 += number[3 * j:(j + 1) * 3] + '-'\n",
    "        else:\n",
    "            return number\n",
    "        if m2 == 0:\n",
    "            n3 += number[3*(m-1):m*3]\n",
    "        elif m2 == 1:\n",
    "            n3 += number[-4:-2] + '-' + number[-2:]\n",
    "        elif m2 == 2:\n",
    "            n3 += number[-5:-2] + '-' + number[-2:]\n",
    "        return n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number1=''\n",
    "        for i in number:\n",
    "            if i!='-' and i!=' ':\n",
    "                number1+=i\n",
    "        n=len(number1)//3\n",
    "        if len(number1)%3==1:\n",
    "            n-=1\n",
    "        ans=''\n",
    "        for i in range(n):\n",
    "            ans+=number1[i*3:i*3+3]+'-'\n",
    "        if len(number1)%3==1:\n",
    "            ans+=number1[-4:-2]\n",
    "            ans+='-'\n",
    "            return ans+number1[len(number1)-2:len(number1)]\n",
    "        elif len(number1)%3==2:\n",
    "            return ans+number1[len(number1)-2:len(number1)]\n",
    "        return ans.strip('-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','')\n",
    "        number=number.replace(' ','')\n",
    "        n=len(number)\n",
    "        part=0\n",
    "        res=''\n",
    "        for i in range(n):\n",
    "            if part<2:\n",
    "                if part==1 and len(number[i+1:])==2:\n",
    "                    #print('if'+str(i))\n",
    "                    res+=number[i]+'-'+number[-2:]\n",
    "                    return res\n",
    "                else:\n",
    "                    res+=number[i]\n",
    "                part+=1\n",
    "            else:\n",
    "                part=0\n",
    "                if i==len(number)-1:\n",
    "                    res+=number[i]\n",
    "                else:\n",
    "                    res+=number[i]+'-'\n",
    "                #print('other'+str(i))\n",
    "           # print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        pos = 0\n",
    "        total = sum(1 for x in number if x !='-' and x !=' ')\n",
    "        need = 0\n",
    "        if total % 3 ==2:\n",
    "            need = total-2\n",
    "        elif total % 3 ==1:\n",
    "            need = total- 2\n",
    "        else:\n",
    "            need = total-3\n",
    "        for i in range(len(number)):\n",
    "            if number[i] != '-' and number[i] !=' ':\n",
    "                ans+=number[i]\n",
    "                pos+=1\n",
    "                if pos<need and pos % 3==0:\n",
    "                    ans += '-'\n",
    "                elif pos == need:\n",
    "                    ans += '-'\n",
    "        \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "\n",
    "        newNum = \"\"\n",
    "\n",
    "\n",
    "        for num in number:\n",
    "\n",
    "\n",
    "            if num.isdigit():\n",
    "                newNum += num\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        res = \"\"\n",
    "\n",
    "        n = len(newNum)\n",
    "        print(newNum)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            left = n - i \n",
    "            if left > 4:\n",
    "                res += newNum[i: i + 3] + \"-\"\n",
    "            else:\n",
    "                break\n",
    "            i += 3\n",
    "        if left == 2 or left == 3:\n",
    "            res += newNum[i:]\n",
    "        elif left == 4:\n",
    "            res +=  newNum[i: i + 2] + \"-\" + newNum[i + 2:]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c in '0123456789')  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "\n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace('-', '').replace(' ', '')\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3:i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][0:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        new_number=[]\n",
    "        for i in number:\n",
    "            if i !=' 'and i!='-':\n",
    "                new_number.append(i)\n",
    "\n",
    "        length=len(new_number)\n",
    "        i=0\n",
    "\n",
    "        output_number=''\n",
    "        left=length\n",
    "        while left>4:\n",
    "            for j in range(3):\n",
    "\n",
    "                output_number+=new_number[i]\n",
    "                i+=1\n",
    "                left-=1\n",
    "            output_number+='-'\n",
    "        if left==4:\n",
    "            output_number+=new_number[-4]\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+='-'\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==3:\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==2:\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        return output_number\n",
    "\n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = \"\".join(c for c in number if c not in (' ', '-'))\n",
    "        idx, tail = None, ''\n",
    "        if len(number) % 3 == 1:\n",
    "            idx = -4\n",
    "            tail = number[-4:-2] + '-' + number[-2:]\n",
    "        elif len(number) % 3 == 2:\n",
    "            idx = -2\n",
    "            tail = number[-2:]\n",
    "\n",
    "        # print(number, idx, tail)\n",
    "\n",
    "        blocks = (b[0]+b[1]+b[2] for b in zip(*(number[i:idx:3] for i in range(3))))\n",
    "        head = \"-\".join(blocks)\n",
    "        return \"-\".join(part for part in (head, tail) if part)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")\n",
    "        _len = len(number)\n",
    "        if _len < 4:\n",
    "            return number\n",
    "        new_number = \"\"\n",
    "        idx = 0\n",
    "        while _len > 4:\n",
    "            new_number += number[idx:idx+3]\n",
    "            new_number += \"-\"\n",
    "            idx += 3\n",
    "            _len -= 3\n",
    "        if _len == 4:\n",
    "            new_number += number[idx:idx+2]\n",
    "            new_number += \"-\"\n",
    "            new_number += number[idx+2:]\n",
    "        \n",
    "        if _len <= 3:\n",
    "            new_number += number[idx:]\n",
    "\n",
    "        return new_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        teleno = ''\n",
    "        for l in number:\n",
    "            if l.isdigit():\n",
    "                teleno += l\n",
    "        #print(teleno)\n",
    "        result = ''\n",
    "        block3 = (len(teleno) - 2) // 3\n",
    "        for i in range(0,block3):\n",
    "            result += teleno[3*i : 3*i+3] + '-'\n",
    "        if len(teleno) - 3*block3 == 2:\n",
    "            result += teleno[-2:]\n",
    "        elif len(teleno) - 3*block3 == 3:\n",
    "            result += teleno[-3:]\n",
    "        elif len(teleno) - 3*block3 == 4:\n",
    "            result += teleno[-4:-2] + '-' + teleno[-2:]\n",
    "        return result\n",
    "\n",
    "\n",
    "# 123-45-67\n",
    "# 123-456-78\n",
    "# 123-456-789\n",
    "# 123-456-78-90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, s: str) -> str:\n",
    "        s = s.replace('-', '').replace(' ', '')\n",
    "        ret = ''\n",
    "        while len(s) > 4:\n",
    "            ret += s[:3] + '-'\n",
    "            s = s[3:]\n",
    "        if len(s) <= 3:\n",
    "            ret += s\n",
    "        else:\n",
    "            ret += s[:2] + '-' + s[2:]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        word = ''\n",
    "        num = ''\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                word +=i\n",
    "            else:\n",
    "                num +=i\n",
    "        if abs(len(word) - len(num)) >1:\n",
    "            return ''\n",
    "        else:\n",
    "            ans = ''\n",
    "            for x in range(min(len(word),len(num))):\n",
    "                if len(word)> len(num):\n",
    "                    ans += word[x]+num[x]\n",
    "                else:\n",
    "                    ans += num[x]+word[x]\n",
    "            if len(word)> len(num):\n",
    "                ans += word[-1]\n",
    "            elif len(word)< len(num):\n",
    "                ans += num[-1]\n",
    "            return ans\n",
    "            \n",
    "            return\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if s.isalpha() or s.isdigit():\n",
    "            if len(s) == 1:\n",
    "                return s\n",
    "            else:\n",
    "                return \"\"\n",
    "        s1 = \"\"\n",
    "        s2 = \"\"\n",
    "        for c in s:\n",
    "            if c >= '0' and c <= '9':\n",
    "                s1 = s1 + c\n",
    "            else:\n",
    "                s2 = s2 + c\n",
    "\n",
    "        res = \"\"\n",
    "        m, n = len(s1), len(s2)\n",
    "        if abs(m-n) >=2:\n",
    "            return \"\"\n",
    "        i = 0\n",
    "        while i < min(m, n):\n",
    "            if m > n:\n",
    "               res = res + s1[i] + s2[i]\n",
    "            else:\n",
    "               res = res + s2[i] + s1[i]\n",
    "            i = i + 1\n",
    "        res = res + s2[i:] + s1[i:]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if len(s) == 1:\n",
    "            return s\n",
    "        t, n = \"\", \"\"\n",
    "        for c in s:\n",
    "            if c.isalpha():\n",
    "                t += c\n",
    "            else:\n",
    "                n += c\n",
    "        a, b, ans = len(t), len(n), \"\"\n",
    "        if abs(b - a) > 1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            if a > b:\n",
    "                for i in range(b):\n",
    "                    ans += (t[i] + n[i])\n",
    "                ans += t[-1]\n",
    "            elif a < b:\n",
    "                for i in range(a):\n",
    "                    ans += (n[i] + t[i])\n",
    "                ans += n[-1]\n",
    "            else:\n",
    "                for i in range(a):\n",
    "                    ans += (t[i] + n[i])\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        alphas, digits, n = [], [], len(s)\n",
    "        for i, c in enumerate(s):\n",
    "            if c.isdigit():\n",
    "                digits.append(c)\n",
    "            else:\n",
    "                alphas.append(c)\n",
    "            if abs(len(digits) - len(alphas)) + i > n:\n",
    "                return \"\"\n",
    "        ans, last_alpha = [], False \n",
    "        while alphas or digits:\n",
    "            m, last_alpha = (alphas, True) if not last_alpha and len(alphas) >= len(digits) else (digits, False)\n",
    "            ans.append(m.pop())\n",
    "        return \"\".join(ans)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        #通过判断是否为数字，将数字和字母分别取出来\n",
    "        aplha,digit=[],[]\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                digit.append(i)\n",
    "            else:\n",
    "                aplha.append(i)\n",
    "      \n",
    "      \n",
    "      \n",
    "        if len(aplha)>len(digit):\n",
    "            aplha,digit=digit,aplha\n",
    "        return \"\" if abs(len(digit)-len(aplha))>1 else \"\".join([x+y for x,y in zip_longest(digit,aplha,fillvalue=\"\")])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        dl,sl=[],[]\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                dl.append(i)\n",
    "            else:\n",
    "                sl.append(i)\n",
    "        #print(dl,sl)                \n",
    "        if len(dl)<=len(sl)+1 and len(dl)>=len(sl)-1:                    \n",
    "            res=[]\n",
    "            if len(dl)>=len(sl):\n",
    "                while dl or sl:                                  \n",
    "                    if dl:\n",
    "                        res.append(dl.pop(0))\n",
    "                    if sl:\n",
    "                        res.append(sl.pop(0))\n",
    "            else:\n",
    "                while dl or sl:                                  \n",
    "                    if sl:\n",
    "                        res.append(sl.pop(0))\n",
    "                    if dl:\n",
    "                        res.append(dl.pop(0))\n",
    "            return ''.join(res)\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if s.isalpha() or s.isdigit():\n",
    "            if len(s) == 1:\n",
    "                return s\n",
    "            else:\n",
    "                return \"\"\n",
    "        s1 = \"\"\n",
    "        s2 = \"\"\n",
    "        for c in s:\n",
    "            if c >= '0' and c <= '9':\n",
    "                s1 = s1 + c\n",
    "            else:\n",
    "                s2 = s2 + c\n",
    "\n",
    "        res = \"\"\n",
    "        m, n = len(s1), len(s2)\n",
    "        if abs(m-n) >=2:\n",
    "            return \"\"\n",
    "\n",
    "        for i in range(min(m, n)):\n",
    "            if m > n:\n",
    "               res = res + s1[i] + s2[i]\n",
    "            else:\n",
    "               res = res + s2[i] + s1[i]\n",
    "\n",
    "        res = res + s2[i+1:n] + s1[i+1:m]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        ans_str = []\n",
    "        ans_num = []\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if 97 <= ord(i) <= 122:\n",
    "                ans_str.append(i)\n",
    "            else:\n",
    "                ans_num.append(i)\n",
    "        n_str = len(ans_str)\n",
    "        n_num = len(ans_num)\n",
    "        if n_num - n_str == 1:\n",
    "            for j in range(n_str):\n",
    "                ans = ans + ans_num[j] + ans_str[j]\n",
    "            ans = ans + ans_num[-1]\n",
    "        elif n_num - n_str == -1:\n",
    "            for j in range(n_num):\n",
    "                ans = ans + ans_str[j] + ans_num[j]\n",
    "            ans = ans + ans_str[-1]\n",
    "        elif n_num - n_str == 0:\n",
    "            for j in range(n_num):\n",
    "                ans = ans + ans_str[j] + ans_num[j]\n",
    "        else:\n",
    "            return ''\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        a,b = [],[]\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                a.append(i)\n",
    "            else:\n",
    "                b.append(i)\n",
    "        res = \"\"\n",
    "        if len(a) != len(b)-1 and len(a)-1 != len(b) and len(a) != len(b):\n",
    "            return res\n",
    "        elif len(a) == len(b)-1:\n",
    "            for p,q in zip(a,b):\n",
    "                res += q\n",
    "                res += p\n",
    "            return res + b[-1]\n",
    "        elif len(a)-1 == len(b):\n",
    "            for p,q in zip(a,b):\n",
    "                res += p\n",
    "                res += q\n",
    "            return res + a[-1]\n",
    "        else:\n",
    "            for p,q in zip(a,b):\n",
    "                res += p\n",
    "                res += q\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        letter = []\n",
    "        number = []\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if ord(i) >= 48 and ord(i) <= 58:\n",
    "                number.append(i)\n",
    "            else:\n",
    "                letter.append(i)\n",
    "        if abs(len(number) - len(letter)) > 1:\n",
    "            return ''\n",
    "        elif len(number) > len(letter):\n",
    "            for i in range(len(letter)):\n",
    "                ans = ans + number[i] + letter[i]\n",
    "            ans = ans + number[-1]\n",
    "        elif len(number) < len(letter):\n",
    "            for i in range(len(number)):\n",
    "                ans = ans + letter[i] + number[i]\n",
    "            ans = ans + letter[-1]\n",
    "        else:\n",
    "            for i in range(len(number)):\n",
    "                ans = ans + letter[i] + number[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        number, character, ans = \"\", \"\", \"\"\n",
    "        for ch in s:\n",
    "            if '0' <= ch <= '9':\n",
    "                number += ch\n",
    "            else:\n",
    "                character += ch\n",
    "        if (len(number) <= len(character) + 1) and (len(number) >= len(character) - 1):\n",
    "            for i in range(min(len(character), len(number))):\n",
    "                ans += character[i]\n",
    "                ans += number[i]\n",
    "            if len(character) > len(number):\n",
    "                ans += character[-1]\n",
    "            elif len(character) < len(number):\n",
    "                ans = number[-1] + ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumdigit = sum(c.isdigit() for c in s)\n",
    "        sumalpha = len(s)-sumdigit\n",
    "        if abs(sumdigit-sumalpha)>1:\n",
    "            return ''\n",
    "        flag = sumdigit>sumalpha\n",
    "        t = list(s)  #list型才好直接修改交换元素\n",
    "        j=1\n",
    "        for i in range(0,len(t),2):\n",
    "            if t[i].isdigit() != flag:#如果数字多,i位为字母;如果字母多,i位为数字,即s[i]为个数少的字符类型时\n",
    "                while t[j].isdigit() !=flag:#若此时j位仍为个数少的类型，则j再往后移动2个直到找到个数多的类型位置，再交换\n",
    "                    j+=2\n",
    "                t[i],t[j]=t[j],t[i] #不在while里了，说明找到了个数多的类型位置，可以交换\n",
    "        return ''.join(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num_list, str_list = [],[]\n",
    "        for i in s:\n",
    "            if i < \"a\":\n",
    "                num_list.append(i)\n",
    "            else:\n",
    "                str_list.append(i)\n",
    "        if abs(len(num_list) - len(str_list)) > 1:\n",
    "            return \"\"\n",
    "        res_str = []\n",
    "        for i in range(min(len(num_list),len(str_list))):\n",
    "            res_str.append(num_list[i])\n",
    "            res_str.append(str_list[i])\n",
    "        if len(num_list) > len(str_list):\n",
    "            res_str.append(num_list[-1])\n",
    "        if len(num_list) < len(str_list):\n",
    "            res_str.reverse()\n",
    "            res_str.append(str_list[-1])\n",
    "            res_str.reverse\n",
    "        return \"\".join(res_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        a = ''\n",
    "        b = ''\n",
    "        c = ''\n",
    "        for i in s:\n",
    "            if 97 <= ord(i) <= 122:\n",
    "                a += i\n",
    "            else:\n",
    "                b += i\n",
    "        if len(a) == len(b):\n",
    "            for x, y in zip(a, b):\n",
    "                c += x\n",
    "                c += y\n",
    "        elif len(a)-len(b) == 1:\n",
    "            for x, y in zip(a, b):\n",
    "                c += x\n",
    "                c += y\n",
    "            c += a[-1]\n",
    "        elif len(b)-len(a) == 1:\n",
    "            for x, y in zip(b, a):\n",
    "                c += x\n",
    "                c += y\n",
    "            c += b[-1] \n",
    "        else:\n",
    "            return '' \n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reformat(self, s: str) -> str:\r\n",
    "        number = list()\r\n",
    "        digit = list()\r\n",
    "        \r\n",
    "        for i in s:\r\n",
    "            if i.isnumeric():\r\n",
    "                number.append(i)\r\n",
    "            else:\r\n",
    "                digit.append(i)\r\n",
    "        if len(number) - len(digit) == 0:\r\n",
    "            ans = [number[j] + digit[j] for j in range(len(number))]\r\n",
    "            return ''.join(ans)\r\n",
    "        elif len(number) - len(digit) == 1:\r\n",
    "            ans = [number[j] + digit[j] for j in range(len(digit))] + [number[-1]]\r\n",
    "            return ''.join(ans)\r\n",
    "        elif len(number) - len(digit) == -1:\r\n",
    "            ans = [digit[j] + number[j] for j in range(len(number))] + [digit[-1]]\r\n",
    "            return ''.join(ans)\r\n",
    "        else:\r\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        # 统计s中的数字和字母的数量，如果差值>1,返回\"\"\n",
    "        # itertools.zip_longest()函数可以将多个迭代器逐个配对，参数fillvalue默认为None\n",
    "        num = re.findall('\\d',s)\n",
    "        string = re.findall('[a-z]',s)\n",
    "        if abs(len(num)-len(string))>1:\n",
    "            return \"\"\n",
    "        m,n = sorted([num,string], key=len)\n",
    "        return \"\".join(map(\"\".join, itertools.zip_longest(n,m,fillvalue=\"\")))\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if s.isalpha() or s.isdigit():\n",
    "            if len(s) == 1:\n",
    "                return s\n",
    "            else:\n",
    "                return \"\"\n",
    "        s1 = \"\"\n",
    "        s2 = \"\"\n",
    "        for c in s:\n",
    "            if c >= '0' and c <= '9':\n",
    "                s1 = s1 + c\n",
    "            else:\n",
    "                s2 = s2 + c\n",
    "\n",
    "        res = \"\"\n",
    "        m, n = len(s1), len(s2)\n",
    "        if abs(m-n) >=2:\n",
    "            return \"\"\n",
    "        i = 0\n",
    "        for i in range(min(m, n)):\n",
    "            if m > n:\n",
    "               res = res + s1[i] + s2[i]\n",
    "            else:\n",
    "               res = res + s2[i] + s1[i]\n",
    "\n",
    "        res = res + s2[i+1:] + s1[i+1:]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if len(s) == 1:\n",
    "            return s\n",
    "        if s.isdigit() or s.isalpha():\n",
    "            return ''\n",
    "        num = ''\n",
    "        zm = ''\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                zm += i\n",
    "            else:\n",
    "                num += i\n",
    "        num_l = len(num)\n",
    "        zm_l = len(zm)\n",
    "        if zm_l == num_l:\n",
    "            res = ''\n",
    "            for i in range(zm_l):\n",
    "                res += zm[i] + num[i]\n",
    "            return res\n",
    "        else:\n",
    "            if abs(zm_l - num_l) != 1:\n",
    "                return ''\n",
    "            else:\n",
    "                if zm_l > num_l:\n",
    "                    res = ''\n",
    "                    for i in range(num_l):\n",
    "                        res += zm[i] + num[i]\n",
    "                    res += zm[-1]    \n",
    "                    return res\n",
    "                else:\n",
    "                    res = ''\n",
    "                    for i in range(zm_l):\n",
    "                        res +=  num[i] + zm[i]\n",
    "                    res += num[-1]    \n",
    "                    return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        sum_digit = sum(c.isdigit() for c in s)\n",
    "        sum_alpha = n-sum_digit\n",
    "        # 数字和字母只能差一个，少的放在奇数位，多的放在偶数位\n",
    "        if abs(sum_digit-sum_alpha) > 1:\n",
    "            return \"\"\n",
    "        flag = sum_digit > sum_alpha\n",
    "        s = list(s)\n",
    "        p, q = 0, 1\n",
    "        ans = [None] * n\n",
    "        while p < n:\n",
    "            if s[p].isdigit() != flag:\n",
    "                while s[q].isdigit() != flag:\n",
    "                    q += 2\n",
    "                s[p], s[q] = s[q], s[p]\n",
    "            p += 2\n",
    "        return ''.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        lis1,lis2=[],[]\n",
    "        res=''\n",
    "        for c in s:\n",
    "            if c>='a' and c<='z':\n",
    "                lis1.append(c)\n",
    "            else:\n",
    "                lis2.append(c)\n",
    "        l1,l2=len(lis1),len(lis2)\n",
    "        l=l1+l2\n",
    "        if abs(l1-l2)>1:\n",
    "            return ''\n",
    "        else:\n",
    "            if l1<=l2:\n",
    "                for i in range(l1):\n",
    "                    res+=lis2[i]\n",
    "                    res+=lis1[i]\n",
    "                if len(res)<l:\n",
    "                    res+=lis2[-1]\n",
    "            else:\n",
    "                for i in range(l2):\n",
    "                    res+=lis1[i]\n",
    "                    res+=lis2[i]\n",
    "                res+=lis1[-1]\n",
    "        return res\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num_list, str_list = [],[]\n",
    "        for i in s:\n",
    "            if i < \"a\":\n",
    "                num_list.append(i)\n",
    "            else:\n",
    "                str_list.append(i)\n",
    "        if abs(len(num_list) - len(str_list)) > 1:\n",
    "            return \"\"\n",
    "        res_str = []\n",
    "        for i in range(min(len(num_list),len(str_list))):\n",
    "            res_str.append(num_list[i])\n",
    "            res_str.append(str_list[i])\n",
    "        if len(num_list) > len(str_list):\n",
    "            res_str.append(num_list[-1])\n",
    "        if len(num_list) < len(str_list):\n",
    "            res_str.reverse()\n",
    "            res_str.append(str_list[-1])\n",
    "            res_str.reverse\n",
    "        return \"\".join(res_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        r,t='',''\n",
    "        for i in s:\n",
    "            if '0'<=i<='9':\n",
    "                r+=i\n",
    "            else:\n",
    "                t+=i\n",
    "        if len(r)<len(t):\n",
    "            r,t=t,r\n",
    "        if abs(len(r)-len(t))>1:\n",
    "            return ''\n",
    "        ans=''\n",
    "        for i in range(len(t)):\n",
    "            ans+=r[i]+t[i]\n",
    "        if len(r)==len(t):\n",
    "            return ans\n",
    "        return ans+r[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumDigit = sum(c.isdigit() for c in s)\n",
    "        sumAlpha = len(s) - sumDigit\n",
    "        if abs(sumDigit - sumAlpha) > 1:\n",
    "            return \"\"\n",
    "        flag = sumDigit > sumAlpha\n",
    "        t = list(s)\n",
    "        j = 1\n",
    "        for i in range(0, len(t), 2):\n",
    "            if t[i].isdigit() != flag:\n",
    "                while t[j].isdigit() != flag:\n",
    "                    j += 2\n",
    "                t[i], t[j] = t[j], t[i]\n",
    "        return ''.join(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        letter = []\n",
    "        number = []\n",
    "        res = []\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                number.append(c)\n",
    "            else:\n",
    "                letter.append(c)\n",
    "        if abs(len(number) - len(letter)) > 1:\n",
    "            return ''\n",
    "        for l in zip_longest(letter, number):\n",
    "            res.extend(l)\n",
    "        if res[-1] == None:\n",
    "            res.pop()\n",
    "            return res\n",
    "        if res[-2] == None:\n",
    "            res.insert(0, res[-1])\n",
    "            return res[:-2]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        a = [w for w in s if w.isnumeric()]\n",
    "        b = [w for w in s if w.islower()]\n",
    "        if len(a)+len(b) != len(s):\n",
    "            return \"\"\n",
    "        if abs(len(a)-len(b)) > 1:\n",
    "            return \"\"\n",
    "        n = min(len(a), len(b))\n",
    "        ans = \"\"\n",
    "        if len(b) == n+1:\n",
    "            ans += b.pop(0)\n",
    "        for _ in range(n):\n",
    "            ans += a.pop(0)\n",
    "            ans += b.pop(0)\n",
    "        if a:\n",
    "            ans += a.pop(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        aplha,digit=[],[]\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                digit.append(i)\n",
    "            else:\n",
    "                aplha.append(i)\n",
    "        if abs(len(digit)-len(aplha))>1:\n",
    "            return \"\"\n",
    "        res=[0]*len(s)\n",
    "        if len(aplha)>len(digit):\n",
    "            res[::2],res[1::2]=aplha,digit\n",
    "        else:\n",
    "            res[::2],res[1::2]=digit,aplha\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        numbers, letters = [], []\n",
    "        for i in s:\n",
    "            if i.isalpha(): letters.append(i)\n",
    "            else: numbers.append(i)\n",
    "        n, l = len(numbers), len(letters)\n",
    "        if abs(n-l)>1: return ''\n",
    "    \n",
    "        def strCmt(numbers, letters, n, l):\n",
    "            res= \"\"\n",
    "            i , j = 0, 0\n",
    "            while i < n or j < l:\n",
    "                if i < n:\n",
    "                    res += numbers[i]\n",
    "                if j < l:\n",
    "                    res += letters[j]\n",
    "                i, j = i + 1, j + 1\n",
    "            return res\n",
    "        return strCmt(numbers, letters, n, l) if n > l else strCmt(letters, numbers, l, n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        count=0\n",
    "        size=len(s)\n",
    "        d=''\n",
    "        a=''\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                count+=1\n",
    "                d+=s[i]\n",
    "            else:\n",
    "                a+=s[i]\n",
    "        if abs(size-count-count)>1:\n",
    "            return ''\n",
    "        res=''\n",
    "        if size-count-count==0:\n",
    "            for j in range(len(a)):\n",
    "                res+=a[j]+d[j]\n",
    "        if size-count-count==1:\n",
    "            for j in range(len(d)):\n",
    "                res+=a[j]+d[j]\n",
    "            res+=a[len(a)-1]\n",
    "        if size-count-count==-1:\n",
    "            for j in range(len(a)):\n",
    "                res+=d[j]+a[j]\n",
    "            res+=d[len(d)-1]\n",
    "        return res\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        chars, nums = \"\", \"\"\n",
    "        for c in s:\n",
    "            if c.isalpha():\n",
    "                chars += c\n",
    "            else:\n",
    "                nums += c\n",
    "        if abs(len(nums) - len(chars)) > 1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            ans = \"\".join([n + c for n, c in zip(nums, chars)])\n",
    "        if len(nums) == len(chars):\n",
    "            return ans\n",
    "        return ans + nums[-1] if len(nums) > len(chars) else chars[-1] + ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sNum = ['0','1','2','3','4','5','6','7','8','9']\n",
    "        ret = list()\n",
    "        res = list()\n",
    "        s1 = []\n",
    "        for v in s:\n",
    "            if v in sNum:\n",
    "                ret.append(v)\n",
    "            else:\n",
    "                res.append(v)\n",
    "        m = len(ret)\n",
    "        n = len(res)\n",
    "        l = len(s)\n",
    "        if m == n:\n",
    "            for i in range(m):\n",
    "                s1.append(ret[i])\n",
    "                s1.append(res[i])\n",
    "            return \"\".join(s1)\n",
    "        if m == n - 1:\n",
    "            for i in range(m):\n",
    "                s1.append(res[i])\n",
    "                s1.append(ret[i])\n",
    "            s1.append(res[-1])\n",
    "            return \"\".join(s1)\n",
    "        if n == m - 1:\n",
    "            for i in range(n):\n",
    "                s1.append(ret[i])\n",
    "                s1.append(res[i])\n",
    "            s1.append(ret[-1]) \n",
    "            return \"\".join(s1)\n",
    "        return \"\"\n",
    "        \n",
    "\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumDigit = sum(c.isdigit() for c in s)\n",
    "        sumAlpha = len(s)-sumDigit\n",
    "        if abs(sumDigit-sumAlpha)>1:\n",
    "            return \"\"\n",
    "        flag = sumDigit>sumAlpha\n",
    "        t= list(s)\n",
    "        j=1\n",
    "        for i in range(0,len(t),2):\n",
    "            if t[i].isdigit() != flag:\n",
    "                while t[j].isdigit() != flag:\n",
    "                    j+=2\n",
    "                t[i],t[j] = t[j],t[i]\n",
    "        return ''.join(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        char1=[ch for ch in s if ch<\"a\"]\n",
    "        char2=[ch for ch in s if ch>=\"a\"]\n",
    "        if len(char2)>len(char1):\n",
    "            char1,char2=char2,char1\n",
    "        if len(char1)-len(char2)>1:\n",
    "            return \"\"\n",
    "        ans=\"\"\n",
    "        for i in range(0,len(char2)):\n",
    "            ans+=char1[i]+char2[i]\n",
    "        if len(char1)>len(char2):\n",
    "            ans+=char1[-1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num_list, str_list = [],[]\n",
    "        for i in s:\n",
    "            if i < \"a\":\n",
    "                num_list.append(i)\n",
    "            else:\n",
    "                str_list.append(i)\n",
    "        if abs(len(num_list) - len(str_list)) > 1:\n",
    "            return \"\"\n",
    "        res_str = []\n",
    "        for i in range(min(len(num_list),len(str_list))):\n",
    "            res_str.append(num_list[i])\n",
    "            res_str.append(str_list[i])\n",
    "        if len(num_list) > len(str_list):\n",
    "            res_str.append(num_list[-1])\n",
    "        if len(num_list) < len(str_list):\n",
    "            res_str.reverse()\n",
    "            res_str.append(str_list[-1])\n",
    "            res_str.reverse\n",
    "        return \"\".join(res_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumDigit = sum(c.isdigit() for c in s)\n",
    "        sumAlpha = len(s) - sumDigit\n",
    "        if abs(sumDigit - sumAlpha) > 1:\n",
    "            return \"\"\n",
    "        flag = sumDigit > sumAlpha\n",
    "        t = list(s)\n",
    "        j = 1\n",
    "        for i in range(0, len(t), 2):\n",
    "            if t[i].isdigit() != flag:\n",
    "                while t[j].isdigit() != flag:\n",
    "                    j += 2\n",
    "                t[i], t[j] = t[j], t[i]\n",
    "        return ''.join(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        digit = 0\n",
    "        alpha = 0\n",
    "        num = []\n",
    "        ch = []\n",
    "        for i in range(n):\n",
    "            if s[i].isdigit():\n",
    "                digit += 1\n",
    "                num.append(s[i])\n",
    "            elif s[i].isalpha():\n",
    "                alpha += 1\n",
    "                ch.append(s[i])\n",
    "        if abs(digit - alpha) > 1:\n",
    "            return \"\"\n",
    "        res = ''\n",
    "        n1 = len(num)\n",
    "        n2 = len(ch)\n",
    "        if digit >= alpha:\n",
    "            i = 0\n",
    "            while i < n1 or i < n2:\n",
    "                if i < n1:\n",
    "                    res += num[i]\n",
    "                if i < n2:\n",
    "                    res += ch[i]\n",
    "                i += 1\n",
    "        else:\n",
    "            i = 0\n",
    "            while i < n1 or i < n2:\n",
    "                if i < n2:\n",
    "                    res += ch[i]\n",
    "                if i < n1:\n",
    "                    res += num[i]\n",
    "                i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        l1, l2 = [], []\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                l1.append(i)\n",
    "            else:\n",
    "                l2.append(i)\n",
    "        if len(l1) == 0 and len(l2) == 0:\n",
    "            return ''\n",
    "        if (len(l1) == 0 and len(l2) > 1) or (len(l2) == 0 and len(l1)>1):\n",
    "            return ''\n",
    "        if (len(l1)==0 and len(l2)==1) or (len(l2)==0 and len(l1)==1):\n",
    "            return s\n",
    "        if abs(len(l1)-len(l2)) >= 2:\n",
    "            return ''\n",
    "        if len(l1) > len(l2):\n",
    "            for i in range(len(l2)):\n",
    "                ans.append(l1[i])\n",
    "                ans.append(l2[i])\n",
    "            ans.append(l1[-1])\n",
    "        elif len(l2) > len(l1):\n",
    "            for i in range(len(l1)):\n",
    "                ans.append(l2[i])\n",
    "                ans.append(l1[i])\n",
    "            ans.append(l2[-1])\n",
    "        else:\n",
    "            for i in range(len(l1)):\n",
    "                ans.append(l1[i])\n",
    "                ans.append(l2[i])\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num_list, str_list = [],[]\n",
    "        for i in s:\n",
    "            if i < \"a\":\n",
    "                num_list.append(i)\n",
    "            else:\n",
    "                str_list.append(i)\n",
    "        if abs(len(num_list) - len(str_list)) > 1:\n",
    "            return \"\"\n",
    "        res_str = []\n",
    "        for i in range(min(len(num_list),len(str_list))):\n",
    "            res_str.append(num_list[i])\n",
    "            res_str.append(str_list[i])\n",
    "        if len(num_list) > len(str_list):\n",
    "            res_str.append(num_list[-1])\n",
    "        if len(num_list) < len(str_list):\n",
    "            res_str.reverse()\n",
    "            res_str.append(str_list[-1])\n",
    "            res_str.reverse\n",
    "        return \"\".join(res_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        s1 = \"\"\n",
    "        s2 = \"\"\n",
    "        for c in s:\n",
    "            if c >= '0' and c <= '9':\n",
    "                s1 = s1 + c\n",
    "            else:\n",
    "                s2 = s2 + c\n",
    "\n",
    "        res = \"\"\n",
    "        m, n = len(s1), len(s2)\n",
    "        if abs(m-n) >=2:\n",
    "            return \"\"\n",
    "        i = 0\n",
    "        while i < min(m, n):\n",
    "            if m > n:\n",
    "               res = res + s1[i] + s2[i]\n",
    "            else:\n",
    "               res = res + s2[i] + s1[i]\n",
    "            i = i + 1\n",
    "        res = res + s2[i:] + s1[i:]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if len(s) == 1:\n",
    "            return s\n",
    "        x = \"\"\n",
    "        y = \"\"\n",
    "        for i in range (len(s)):\n",
    "            if s[i].isdigit():\n",
    "                x += s[i]\n",
    "            else:\n",
    "                y += s[i]\n",
    "        if len(x) == 0 or len(y) == 0:\n",
    "            return \"\"\n",
    "        if abs(len(x) - len(y)) >= 2:\n",
    "            return \"\"\n",
    "        elif abs(len(x) - len(y)) == 0:\n",
    "            target = \"\"\n",
    "            for i in range (len(x)):\n",
    "                target += x[i] + y[i]\n",
    "            return target\n",
    "        else:\n",
    "            if len(x) > len(y):\n",
    "                target = x[-1]\n",
    "                for i in range (len(y)):\n",
    "                    target += y[i] + x[i]\n",
    "                return target\n",
    "            else:\n",
    "                target = y[-1]\n",
    "                for i in range (len(x)):\n",
    "                    target += x[i] + y[i]\n",
    "                return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumDigit = sum(c.isdigit() for c in s)\n",
    "        sumAlpha = len(s) - sumDigit\n",
    "        if abs(sumDigit - sumAlpha) > 1:\n",
    "            return \"\"\n",
    "        flag = sumDigit > sumAlpha\n",
    "        t = list(s)\n",
    "        j = 1\n",
    "        for i in range(0, len(t), 2):\n",
    "            if t[i].isdigit() != flag:\n",
    "                while t[j].isdigit() != flag:\n",
    "                    j += 2\n",
    "                t[i], t[j] = t[j], t[i]\n",
    "        return ''.join(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        if len(s) == 1:\n",
    "            return s\n",
    "        a, b, n, t = 0, 0, \"\", \"\"\n",
    "        for c in s:\n",
    "            if c.isalpha():\n",
    "                a += 1\n",
    "                t += c\n",
    "            else:\n",
    "                b += 1\n",
    "                n += c\n",
    "        if abs(b - a) > 1:\n",
    "            return \"\"\n",
    "        ans = \"\"\n",
    "        if a > b:\n",
    "            for i in range(b):\n",
    "                ans += (t[i] + n[i])\n",
    "            ans += t[-1]\n",
    "        elif a < b:\n",
    "            for i in range(a):\n",
    "                ans += (n[i] + t[i])\n",
    "            ans += n[-1]\n",
    "        else:\n",
    "            for i in range(a):\n",
    "                ans += (t[i] + n[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        ch = []\n",
    "        num = []\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                ch.append(i)\n",
    "            else:\n",
    "                num.append(i)\n",
    "        if abs(len(ch)-len(num))>1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            res = \"\"\n",
    "            if len(ch) == len(num):\n",
    "                for i in range(len(ch)):\n",
    "                    res += ch[i]+num[i]\n",
    "                return res\n",
    "            elif len(ch) > len(num):\n",
    "                for i in range(len(num)):\n",
    "                    res += ch[i]+num[i]\n",
    "                res += ch[-1]\n",
    "                return res\n",
    "            else:\n",
    "                for i in range(len(ch)):\n",
    "                    res += num[i]+ch[i]\n",
    "                res += num[-1]\n",
    "                return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        sumDigit = sum(c.isdigit() for c in s)\n",
    "        sumAlpha = len(s) - sumDigit\n",
    "        if abs(sumDigit - sumAlpha) > 1:\n",
    "            return \"\"\n",
    "        flag = sumDigit > sumAlpha\n",
    "        t = list(s)\n",
    "        j = 1\n",
    "        for i in range(0, len(t), 2):\n",
    "            if t[i].isdigit() != flag:\n",
    "                while t[j].isdigit() != flag:\n",
    "                    j += 2\n",
    "                t[i], t[j] = t[j], t[i]\n",
    "        return ''.join(t)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        char = re.findall('\\D',s)  # 用正则表达式匹配出所有非数字\n",
    "        digit = re.findall('\\d',s) # 用正则表达式匹配出所有数字\n",
    "        if abs(len(char)-len(digit))>1:return '' # 如果字母数量与数字数量相差超过1，\n",
    "                                                # 则无法按要求组合，返回空字符串\n",
    "        res =''\n",
    "        if len(s)==1:return s\n",
    "        if len(char)>len(digit):  # 根据字母数量与数字数量重新组合字符串\n",
    "            for a in range(len(char)-1):\n",
    "                res = res + char[a]+digit[a]\n",
    "            res = res + char[a+1]\n",
    "        elif len(char)<len(digit):\n",
    "            for a in range(len(digit)-1):\n",
    "                res =res + digit[a]+char[a]\n",
    "            res = res + digit[a+1]\n",
    "        else: \n",
    "            for a in range(len(digit)):\n",
    "                res =res + digit[a]+char[a]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        nums = []\n",
    "        ch = []\n",
    "        res = \"\"\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                nums.append(c)\n",
    "            else:\n",
    "                ch.append(c)\n",
    "        \n",
    "        if abs(len(nums) - len(ch)) > 1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            if len(ch) == len(nums):\n",
    "                for i in range(len(ch)):\n",
    "                    res += nums[i]\n",
    "                    res += ch[i]\n",
    "            elif len(ch) > len(nums):\n",
    "                for i in range(len(nums)):\n",
    "                    res += ch[i]\n",
    "                    res += nums[i]\n",
    "                res += ch[-1]\n",
    "            else:\n",
    "                for i in range(len(ch)):\n",
    "                    res += nums[i]\n",
    "                    res += ch[i]\n",
    "                res += nums[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        shuzi = []\n",
    "        zimu = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isalpha():\n",
    "                zimu.append(s[i])\n",
    "            else:\n",
    "                shuzi.append(s[i])\n",
    "        length1 = len(shuzi)\n",
    "        length2 = len(zimu)\n",
    "        if abs(length1-length2)>=2:\n",
    "            return \"\"\n",
    "        else:\n",
    "            res = \"\"\n",
    "            if length1>length2:\n",
    "                for i in range(length2):\n",
    "                    res = res+shuzi[i]\n",
    "                    res = res+zimu[i]\n",
    "                res = res+shuzi[-1]\n",
    "            elif length2>length1:\n",
    "                for i in range(length1):\n",
    "                    res = res+zimu[i]\n",
    "                    res = res+shuzi[i]\n",
    "                res = res+zimu[-1]\n",
    "            else:\n",
    "                for i in range(length1):\n",
    "                    res = res+shuzi[i]+zimu[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        letter = []\n",
    "        number = []\n",
    "        res = []\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                number.append(c)\n",
    "            else:\n",
    "                letter.append(c)\n",
    "        if abs(len(number) - len(letter)) > 1:\n",
    "            return ''\n",
    "        for l in zip_longest(letter, number):\n",
    "            res.extend(l)\n",
    "        if res[-1] == None:\n",
    "            return res[:-1]\n",
    "        if res[-2] == None:\n",
    "            res.insert(0, res[-1])\n",
    "            return res[:-2]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num_count = []\n",
    "        char_count = []\n",
    "        for i, ch in enumerate(s):\n",
    "            if ord('0') <= ord(ch) <= ord('9'):\n",
    "                num_count.append(i)\n",
    "            else:\n",
    "                char_count.append(i)\n",
    "        if abs(len(num_count) - len(char_count)) > 1: return \"\"\n",
    "        ans = \"\"\n",
    "        if len(num_count) > len(char_count):\n",
    "            for i in range(min(len(num_count), len(char_count))):\n",
    "                ans += s[num_count[i]] + s[char_count[i]]\n",
    "            ans += s[num_count[-1]]\n",
    "        elif len(num_count) < len(char_count):\n",
    "            for i in range(min(len(num_count), len(char_count))):\n",
    "                ans += s[char_count[i]] + s[num_count[i]]\n",
    "            ans += s[char_count[-1]]\n",
    "        else:\n",
    "            for i in range(min(len(num_count), len(char_count))):\n",
    "                ans += s[char_count[i]] + s[num_count[i]]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        s1 = \"\"\n",
    "        s2 = \"\"\n",
    "        for c in s:\n",
    "            if c >= '0' and c <= '9':\n",
    "                s1 = s1 + c\n",
    "            else:\n",
    "                s2 = s2 + c\n",
    "\n",
    "        res = \"\"\n",
    "        m, n = len(s1), len(s2)\n",
    "        if abs(m-n) >=2:\n",
    "            return \"\"\n",
    "        \n",
    "        i = 0\n",
    "        while i < min(m, n):\n",
    "            if m > n:\n",
    "               res = res + s1[i] + s2[i]\n",
    "            else:\n",
    "               res = res + s2[i] + s1[i]\n",
    "            i = i + 1\n",
    "        \n",
    "        res = res + s2[i:] + s1[i:]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        numbers=[]\n",
    "        letters=[]\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                letters.append(i)\n",
    "            else:\n",
    "                numbers.append(i)\n",
    "        n,l=len(numbers),len(letters)\n",
    "        if abs(n-l)>1:\n",
    "            return ''\n",
    "        res=[]\n",
    "        if n>=l:\n",
    "            while numbers:\n",
    "                res.append(numbers.pop())\n",
    "                if letters:\n",
    "                    res.append(letters.pop())\n",
    "        else:\n",
    "            while letters:\n",
    "                res.append(letters.pop())\n",
    "                if numbers:\n",
    "                    res.append(numbers.pop())\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num=0\n",
    "        num1=[]\n",
    "        letter=0\n",
    "        letter1=[]\n",
    "        ans=''\n",
    "        for i in s:\n",
    "            if i>='0' and i<='9':\n",
    "                num+=1\n",
    "                num1.append(i)\n",
    "            elif i>='a' and i<='z':\n",
    "                letter+=1\n",
    "                letter1.append(i)\n",
    "        if abs(num-letter)>1:\n",
    "            ans=''\n",
    "        else:\n",
    "            if num>letter:\n",
    "                for j in range(letter):\n",
    "                    ans+=num1[j]\n",
    "                    ans+=letter1[j]\n",
    "                ans+=num1[-1]\n",
    "            elif num<letter:\n",
    "                for j in range(num):\n",
    "                    ans+=letter1[j]\n",
    "                    ans+=num1[j]\n",
    "                ans+=letter1[-1]\n",
    "            else:\n",
    "                for j in range(num):\n",
    "                    ans+=letter1[j]\n",
    "                    ans+=num1[j]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        zimu = []\n",
    "        num = []\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            for j in range(48,59):\n",
    "                if ord(i) == j:\n",
    "                    num.append(int(i))\n",
    "            for j in range(97,123):\n",
    "                if ord(i) == j:\n",
    "                    zimu.append(i)\n",
    "\n",
    "        if len(num)==len(zimu)+1:\n",
    "            for i in range(len(zimu)):\n",
    "                ans = ans + str(num[i])\n",
    "                ans = ans+str(zimu[i])\n",
    "            ans = ans+str(num[-1])\n",
    "            return ans\n",
    "        elif len(num)+1==len(zimu):\n",
    "            for i in range(len(num)):\n",
    "                ans = ans+str(zimu[i])\n",
    "                ans = ans+str(num[i])\n",
    "            ans = ans+str(zimu[-1])\n",
    "            return ans\n",
    "        elif len(num) == len(zimu):\n",
    "            for i in range(len(num)):\n",
    "                ans = ans+str(num[i])\n",
    "                ans = ans+str(zimu[i])\n",
    "            return ans\n",
    "        else:return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        a, b = [], []\n",
    "        for c in s:\n",
    "            if c in '0123456789':\n",
    "                a.append(c)\n",
    "            else:\n",
    "                b.append(c)\n",
    "        if abs(len(a) - len(b)) > 1:\n",
    "            return ''\n",
    "        if len(a) == len(b):\n",
    "            return ''.join(a[i] + b[i] for i in range(len(a)))\n",
    "        if len(a) > len(b):\n",
    "            return ''.join(a[i] + b[i] for i in range(len(b))) + a[-1]\n",
    "        return ''.join(b[i] + a[i] for i in range(len(a))) + b[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        alpha=''.join([i for i in s if i.encode('UTF-8').isalpha()])\n",
    "        num=''.join([i for i in s if i.isdigit()])\n",
    "        print(alpha)\n",
    "        print(num)\n",
    "        if abs(len(alpha)-len(num))>1:\n",
    "            return \"\"\n",
    "        if len(alpha)==len(num):\n",
    "            return  ''.join(alph+num for alph,num in zip(alpha,num))\n",
    "        return ''.join(alph+num for alph,num in zip(alpha,num))+alpha[-1] if len(alpha) > len(num) else num[-1]+''.join(alph+num for alph,num in zip(alpha,num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        letter = []\n",
    "        number = []\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                number.append(c)\n",
    "            else:\n",
    "                letter.append(c)\n",
    "        if abs(len(number) - len(letter)) > 1:\n",
    "            return ''\n",
    "        \n",
    "        res = []\n",
    "        if len(number) > len(letter):\n",
    "            letter, number = number,letter\n",
    "        for l in zip_longest(letter, number):\n",
    "            res.extend(l)\n",
    "        if res[-1] == None:\n",
    "            return res[:-1]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        ch = []\n",
    "        num = []\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                ch.append(i)\n",
    "            else:\n",
    "                num.append(i)\n",
    "        if abs(len(ch)-len(num))>1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            res = \"\"\n",
    "            if len(ch) == len(num):\n",
    "                for i in range(len(ch)):\n",
    "                    res += ch[i]+num[i]\n",
    "                return res\n",
    "            elif len(ch) > len(num):\n",
    "                for i in range(len(num)):\n",
    "                    res += ch[i]+num[i]\n",
    "                res += ch[-1]\n",
    "                return res\n",
    "            else:\n",
    "                for i in range(len(ch)):\n",
    "                    res += num[i]+ch[i]\n",
    "                res += num[-1]\n",
    "                return res\n",
    "            \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        letter = []\n",
    "        digit = []\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                digit.append(i)\n",
    "            else:\n",
    "                letter.append(i)\n",
    "        if abs(len(digit) - len(letter)) > 1:\n",
    "            return ''\n",
    "        result = ''\n",
    "        if len(digit) > len(letter):\n",
    "            for i in range(0,len(letter)):\n",
    "                result += digit[i] + letter[i]\n",
    "            result += digit[-1]\n",
    "        elif len(digit) < len(letter):\n",
    "            for i in range(0,len(digit)):\n",
    "                result += letter[i] + digit[i]\n",
    "            result += letter[-1]\n",
    "        else:\n",
    "            for i in range(0,len(letter)):\n",
    "                result += digit[i] + letter[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        alpha, digits = [], []\n",
    "        for ch in s:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "            else:\n",
    "                alpha.append(ch)\n",
    "        \n",
    "        if abs(len(alpha)-len(digits)) > 1:\n",
    "            return \"\"\n",
    "        \n",
    "        res = \"\"\n",
    "        i, j = 0, 0\n",
    "        while i < len(alpha) and j < len(digits):\n",
    "            res += alpha[i]\n",
    "            i += 1\n",
    "            res += digits[j]\n",
    "            j += 1\n",
    "        res += ''.join(alpha[i:])\n",
    "        \n",
    "        res = ''.join(digits[j:]) + res\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        s1 = []\n",
    "        s2 = []\n",
    "        res = \"\"\n",
    "        for ch in s:\n",
    "            if ch.isalpha():s1.append(ch)\n",
    "            else:s2.append(ch)\n",
    "        if abs(len(s1)-len(s2))<=1:\n",
    "            if len(s1)<=len(s2):\n",
    "                ll = len(s2)\n",
    "                while ll>0:\n",
    "                    if s2:\n",
    "                        res += s2[0]\n",
    "                        del s2[0]\n",
    "                    if s1:\n",
    "                        res += s1[0]\n",
    "                        del s1[0]\n",
    "                    ll -= 1\n",
    "                return res\n",
    "            else:\n",
    "                ll = len(s1)\n",
    "                while ll>0:\n",
    "                    if s1:\n",
    "                        res += s1[0]\n",
    "                        del s1[0]\n",
    "                    if s2:\n",
    "                        res += s2[0]\n",
    "                        del s2[0]\n",
    "                    ll -= 1\n",
    "                return res\n",
    "        else:return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        digits = []\n",
    "        letters = []\n",
    "        \n",
    "        # 分类存储数字和字母\n",
    "        for char in s:\n",
    "            if char.isnumeric():\n",
    "                digits.append(char)\n",
    "            else:\n",
    "                letters.append(char)\n",
    "        \n",
    "        # 如果两个数组的长度之差大于 1，返回空字符串\n",
    "        if abs(len(digits) - len(letters)) > 1:\n",
    "            return \"\"\n",
    "        \n",
    "        res = []\n",
    "        \n",
    "        # 确保从长度较长的数组开始\n",
    "        if len(digits) >= len(letters):\n",
    "            for d, l in zip(digits, letters):\n",
    "                res.append(d)\n",
    "                res.append(l)\n",
    "            if len(digits) > len(letters):\n",
    "                res.append(digits[-1])\n",
    "        else:\n",
    "            for l, d in zip(letters, digits):\n",
    "                res.append(l)\n",
    "                res.append(d)\n",
    "            if len(letters) > len(digits):\n",
    "                res.append(letters[-1])\n",
    "        \n",
    "        return \"\".join(res)\n",
    "\n",
    "# 测试\n",
    "sol = Solution()\n",
    "print(sol.reformat(\"a0b1c2\"))  # 输出 \"0a1b2c\"\n",
    "print(sol.reformat(\"leetcode\"))  # 输出 \"\"\n",
    "print(sol.reformat(\"1229857369\"))  # 输出 \"\"\n",
    "print(sol.reformat(\"covid2019\"))  # 输出 \"c2o0v1i9d\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        alphas, digits, n = [], [], len(s)\n",
    "        for i, c in enumerate(s):\n",
    "            if c.isdigit():\n",
    "                digits.append(c)\n",
    "            else:\n",
    "                alphas.append(c)\n",
    "            # 以下几种判断方式都可以\n",
    "            # if 2 * max(len(digits), len(alphas)) > n + 1:\n",
    "            # if min(len(alphas), len(digits)) + n - i < max(len(alphas), len(digits)):\n",
    "            if abs(len(digits) - len(alphas)) + i > n:\n",
    "                return \"\"\n",
    "        ans, last_alpha = [], False\n",
    "        while alphas or digits:\n",
    "            if not last_alpha and len(alphas) >= len(digits):\n",
    "                m, last_alpha = (alphas, True)\n",
    "            else:\n",
    "                m, last_alpha = (digits, False)\n",
    "            ans.append(m.pop())\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        result = ''\n",
    "        nums_arr = list()\n",
    "        char_arr = list()\n",
    "        for c in s:\n",
    "            tmp = ord(c)\n",
    "            if 57 >= tmp >= 48:\n",
    "                nums_arr.append(c)\n",
    "            else:\n",
    "                char_arr.append(c)\n",
    "        len_nums, len_char = len(nums_arr), len(char_arr)\n",
    "        len_result = len_nums + len_char\n",
    "        if abs(len_nums - len_char) > 1:\n",
    "            return result\n",
    "        while len_result != len(result):\n",
    "            len_nums -= 1\n",
    "            len_char -= 1\n",
    "            if len_nums > len_char:\n",
    "                result += nums_arr[len_nums] if len_nums >= 0 else ''\n",
    "                result += char_arr[len_char] if len_char >= 0 else ''\n",
    "            else:\n",
    "                result += char_arr[len_char] if len_char >= 0 else ''\n",
    "                result += nums_arr[len_nums] if len_nums >= 0 else ''\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        s=[i for i in s]\n",
    "        aa=[i for i in s if i.isalpha()]\n",
    "        bb=[i for i in s if i.isdigit()]\n",
    "        result=[]\n",
    "        if abs(len(aa)-len(bb))>=2:\n",
    "            return ''\n",
    "        elif len(aa)>len(bb):\n",
    "            for i in range(len(bb)):\n",
    "                result.append(aa[i])\n",
    "                result.append(bb[i])\n",
    "            result=result+[aa[-1]]\n",
    "            return ''.join(result)\n",
    "        elif len(bb)>len(aa):\n",
    "            for i in range(len(aa)):\n",
    "                result.append(bb[i])\n",
    "                result.append(aa[i])\n",
    "            result=result+[bb[-1]]\n",
    "            return ''.join(result)\n",
    "        elif len(bb)==len(aa):\n",
    "            for i in range(len(aa)):\n",
    "                result.append(bb[i])\n",
    "                result.append(aa[i])\n",
    "            return ''.join(result)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformat(self, s: str) -> str:\n",
    "        num=[]\n",
    "        char=[]\n",
    "        result=[1]*len(s)\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                num.append(i)\n",
    "            else:\n",
    "                char.append(i)\n",
    "        if len(num)==0 and len(char)>1:\n",
    "            return \"\"\n",
    "        if len(char)==0 and len(num)>1:\n",
    "            return \"\"\n",
    "        if len(num)-len(char)>1 or len(char)-len(num)>1:\n",
    "            return \"\"\n",
    "        if len (char)>=len(num):\n",
    "            for i in range (len(char)):\n",
    "                result[i*2]=char[i]\n",
    "            for i in range(len(num)):\n",
    "                result[i*2+1]=num[i]\n",
    "        if len (num)>len(char):\n",
    "            for i in range (len(num)):\n",
    "                result[i*2]=num[i]\n",
    "            for i in range(len(char)):\n",
    "                result[i*2+1]=char[i]\n",
    "\n",
    "        return result\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
